【测评】读入效率测评第二弹

上次测评,有大佬说我测评的结果不真实。因为从不同的文件读取,文件在硬盘中位置不同,可能会受影响。所以今天我在整一波真实的。

代码

数据生成器

#include <cstdlib>
#include <cstdio>
 
int main() {
    FILE *out = fopen("data.txt", "w");
    for (int i = 1; i <= 1e7; i++) {
        int x = rand();
        fprintf(out, "%d\n", x);
    }
    return 0;
}

跑时间程序

#include <bits/stdc++.h>
 
using namespace std;
 
char rbuf[1 << 20]; char *p1 = rbuf, *p2 = rbuf;
 
char gc() {
    if (p1 == p2) {
        p1 = rbuf;
        p2 = rbuf + fread(rbuf, 1, 1 << 20, stdin);
    }
    return p1 == p2 ? EOF : *p1++;
}
 
int read() {
    register int f = 1, x = 0; register char ch = gc();
    while (!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = gc();
    while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = gc();
    return f * x;
}
 
int qread() {
    register int f = 1, x = 0; register char ch = gc();
    while (!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = gc();
    while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = gc();
    return f * x;
}
 
double Getchar() {
    double s = clock();
    for (int i = 1, x; i <= 1e7; i++) x = qread();
    double t = clock();
    return t - s;
}
 
double Fread() {
    double s = clock();
    for (int i = 1, x; i <= 1e7; i++) x = read();
    double t = clock();
    return t - s;
}
 
double Scanf() {
    double s = clock();
    for (int i = 1, x; i <= 1e7; i++) scanf("%d", &x);
    double t = clock();
    return t - s;
}
 
double Cin() {
    double s = clock();
    for (int i = 1, x; i <= 1e7; i++) cin >> x;
    double t = clock();
    return t - s;
}
 
double FastCin() {
    ios :: sync_with_stdio(false); cin.tie(NULL);
    double s = clock();
    for (int i = 1, x; i <= 1e7; i++) cin >> x;
    double t = clock();
    return t - s;
}
 
int main() {
    freopen("data.txt", "r", stdin);
    freopen("res.txt", "w", stdout);
    printf("getchar: %lf\n", Getchar());
    fclose(stdin);
    freopen("data.txt", "r", stdin);
    printf("fread: %lf\n", Fread());
    fclose(stdin);
    freopen("data.txt", "r", stdin);
    printf("scanf: %lf\n", Scanf());
    fclose(stdin);
    freopen("data.txt", "r", stdin);
    printf("cin: %lf\n", Cin());
    fclose(stdin);
    freopen("data.txt", "r", stdin);
    printf("fastcin: %lf\n", FastCin());
    return 0;
}

测评结果

把能杀死的进程都杀死了,跑这个代码。

跑$1e7$

第一次$\downarrow$

getchar: 312.000000
fread: 281.000000
scanf: 4796.000000
cin: 6186.000000
fastcin: 1656.000000

第二次$\downarrow$

getchar: 296.000000
fread: 282.000000
scanf: 4655.000000
cin: 5998.000000
fastcin: 1344.000000

第三次$\downarrow$

getchar: 328.000000
fread: 312.000000
scanf: 5358.000000
cin: 11372.000000
fastcin: 4171.000000

第四次$\downarrow$

getchar: 374.000000
fread: 313.000000
scanf: 5577.000000
cin: 11372.000000
fastcin: 4187.000000

其中前两次是64位程序跑出来的,后两次是32位程序跑出来的

跑$3e7$

getchar: 968.000000
fread: 1031.000000
scanf: 16028.000000
cin: 33927.000000
fastcin: 12513.000000
getchar: 984.000000
fread: 953.000000
scanf: 16543.000000
cin: 33929.000000
fastcin: 12560.000000

两次都是32位程序跑出来的