C++各个函数读写速度一览

发布于 2018-10-13  526 次阅读


cin函数测试有问题!!!仅供参考!!!

(有bug)各个函数的读入速度

整数和字符

cpu:i7-7700HQ g++ 6.3.0

名称 10^5整数 10^6整数 10^7整数 10M字符串
fin 66毫秒 692毫秒 6831毫秒 117毫秒
cin 108毫秒 45毫秒 425毫秒 1毫秒
cin关同步 76毫秒 40毫秒 397毫秒 77毫秒
scanf 29毫秒 287毫秒 2838毫秒 75毫秒
FILE* fscanf 29毫秒 280毫秒 2818毫秒 68毫秒
快读 4毫秒 44毫秒 463毫秒

代码

#include <iostream>
#include <fstream>
#include <cstdio>
#include <ctime>

using namespace std;

char s[10000005];
int x[10000005];

void tfst(string name, int n)
{
    ifstream fin(name.c_str());
    for (int i = 0; i <= n; i++)
        fin >> x[i];
    fin.close();
}

void tfsts(string name)
{
    ifstream fin(name.c_str());
    fin >> s;
    fin.close();
}

void tfreSan(string name, int n)
{
    freopen(name.c_str(), "r", stdin);
    for (int i = 0; i <= n; i++)
        scanf("%d", &x[i]);
    fclose(stdin);
}

void tfresSan(string name)
{
    freopen(name.c_str(), "r", stdin);
    scanf("%s", s);
    fclose(stdin);
}

void tfreFSan(string name, int n)
{
    FILE *fin;
    fin = fopen(name.c_str(), "r");
    for (int i = 0; i <= n; i++)
        fscanf(fin, "%d", &x[i]);
    fclose(fin);
}

void tfresFSan(string name)
{
    FILE *fin;
    fin = fopen(name.c_str(), "r");
    fscanf(fin, "%s", s);
    fclose(fin);
}

void tfreCin(string name, int n)
{
    freopen(name.c_str(), "r", stdin);
    for (int i = 0; i <= n; i++)
        cin >> x[i];
    fclose(stdin);
}

void tfresCin(string name)
{
    freopen(name.c_str(), "r", stdin);
    cin >> s;
    fclose(stdin);
}

void tfreCinS(string name, int n)
{
    freopen(name.c_str(), "r", stdin);
    std::ios::sync_with_stdio(false);
    for (int i = 0; i <= n; i++)
        cin >> x[i];
    std::ios::sync_with_stdio(true);
    fclose(stdin);
}

void tfresCinS(string name)
{
    freopen(name.c_str(), "r", stdin);
    std::ios::sync_with_stdio(false);
    scanf("%s", s);
    std::ios::sync_with_stdio(true);
    fclose(stdin);
}

inline int qr()
{
    int data = 0, k = 1;
    char c;

    for (c = getchar(); (c > '9' || c < '0') && c == '-'; c = getchar())
        if (c == '-')
            k = -1;

    for (; '0' <= c && c <= '9'; c = getchar())
        data = data * 10 + c - '0';
    return data * k;
}

void makeInt(string name, int n)
{
    ofstream fout(name.c_str());
    for (int i = 1; i <= n; i++)
        fout << rand() << " ";
    fout.close();
}

void tq(string name, int n)
{
    freopen(name.c_str(), "r", stdin);
    for (int i = 0; i <= n; i++)
        x[i] = qr();
    fclose(stdin);
}

void makeChar(string name)
{
    freopen(name.c_str(), "w", stdout);
    for (int i = 1; i <= 10000000; i++)
        putchar('%');
    fclose(stdout);
}

int main()
{
    srand(time(0));
    int st, et;

    makeInt("d1.in", 100000);
    makeInt("d2.in", 1000000);
    makeInt("d3.in", 10000000);
    makeChar("d4.in");

    freopen("out.txt", "w", stdout);

    st = clock();
    tfst("d1.in", 100000);
    et = clock();

    cout << "fin 10^5耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfst("d2.in", 1000000);
    et = clock();

    cout << "fin 10^6耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfst("d3.in", 10000000);
    et = clock();

    cout << "fin 10^7耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfsts("d4.in");
    et = clock();

    cout << "fin 10M耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreCin("d1.in", 100000);
    et = clock();

    cout << "cin 10^5耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreCin("d2.in", 1000000);
    et = clock();

    cout << "cin 10^6耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreCin("d3.in", 10000000);
    et = clock();

    cout << "cin 10^7耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfresCin("d4.in");
    et = clock();

    cout << "cin 10M耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreCinS("d1.in", 100000);
    et = clock();

    cout << "cin sync_with_stdio(false) 10^5耗时" << (double) (et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreCinS("d2.in", 1000000);
    et = clock();

    cout << "cin sync_with_stdio(false) 10^6耗时" << (double) (et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreCinS("d3.in", 10000000);
    et = clock();

    cout << "cin sync_with_stdio(false) 10^7耗时" << (double) (et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfresCinS("d4.in");
    et = clock();

    cout << "cin sync_with_stdio(false) 10M耗时" << (double) (et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreSan("d1.in", 100000);
    et = clock();

    cout << "scanf 10^5耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreSan("d2.in", 1000000);
    et = clock();

    cout << "scanf 10^6耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreSan("d3.in", 10000000);
    et = clock();

    cout << "scanf 10^7耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfresSan("d4.in");
    et = clock();

    cout << "scanf 10M耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreFSan("d1.in", 100000);
    et = clock();

    cout << "FILE* fscanf 10^5耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreFSan("d2.in", 1000000);
    et = clock();

    cout << "FILE* fscanf 10^6耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfreFSan("d3.in", 10000000);
    et = clock();

    cout << "FILE* fscanf 10^7耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tfresFSan("d4.in");
    et = clock();

    cout << "FILE* fscanf 10M耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tq("d1.in", 100000);
    et = clock();

    cout << "快读 10^5耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tq("d2.in", 1000000);
    et = clock();

    cout << "快读 10^6耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    st = clock();
    tq("d3.in", 10000000);
    et = clock();

    cout << "快读 10^7耗时" << (double)(et - st) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;

    return 0;
}

写在最后

其他人测的结果:“cin总是最慢的,cin关同步和scanf差不多”。

总之。。。

快读大fa好orz。