版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chr1991/article/details/80945496
我的实验机器配置是:
- 处理器:Intel(R) Core(TM) i3-7100U CPU @ 2.40GHz 2.40GHz
- 随机访问存储器:4.00GB
- 操作系统:Windows10
- 集成开发环境:Visual Studio 2017
我将stdin与输入文件链接在一起,依次在104、105、106、107量级的数据上进行测试,得到结果如下。
画成柱状图如下:
以10000数量级的时间为1,可得到这样的表格:
cin/scanf的时间比如下:
扫描二维码关注公众号,回复:
4525156 查看本文章
由此可见,cin读入相同数据的时间是scanf的3.5~4倍。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <ctime>
#include <cstdio>
using namespace std;
const int N = 10000; // modify this to change the size of data
int main()
{
clock_t start;
FILE* ptr = freopen("data.out", "rb", stdin);
start = clock();
for (int cnt = 0; cnt < N; cnt++) {
int tmp;
// cin >> tmp; // remove the comment symbol to test on cin
scanf("%d", &tmp);
}
clock_t end;
end = clock();
double seconds = (double)(end - start) / CLOCKS_PER_SEC;
printf("%lf\n", seconds);
fclose(stdin);
return 0;
}