每日一题7:速度
题目描述
信息学比赛中有时输入数据很大,数据文件的读取方式和效率就要重视了。一般地,使用scanf、printf比使用cin、cout速度快。请根据以下测试表,重新设计方案,对各种方式的文件进行速度测试。可适当在输出部分提示测试方案以及结果。
读入方式 | 10^5个整数时间(ms) | 10^6个整数时间(ms) | 10^7个整数时间(ms) |
---|---|---|---|
ifstream+fin | 16 | 103 | 1001 |
FILE*+scanf | 55 | 345 | 3366 |
freopen+cin | 67 | 440 | 4378 |
freopen+cin+sync_with_stdio(false) | 9 | 103 | 1024 |
freopen+scanf | 32 | 318 | 3112 |
读入方式 | 10^5个整数时间(ms) | 10^6个整数时间(ms) | 10^7个整数时间(ms) |
---|---|---|---|
ifstream+fin | 65 | 982 | 6148 |
FILE*+scanf | 53 | 692 | 4829 |
freopen+cin | 727 | 6450 | 62738 |
freopen+cin+sync_with_stdio(false) | 73 | 674 | 6533 |
freopen+scanf | 55 | 510 | 4579 |
输入格式
无。
输出格式
可自行定义。
例如:"10^5 integer:"
分析
按题目要求用各种方案检测即可。
代码
#include<bits/stdc++.h>
using namespace std;
int x[10000005];
char s[10000005];
void test(string nm,int n){
ifstream fin(nm.c_str());
fin>>n;
for(int i=0;i<n;i++)
fin>>x[i];
fin.close();
}
int main(){
srand(int(time(NULL)));
int s,e;
s=clock();
test("d1.in",100000);
e=clock();
cout<<"10^5 integer:"<<e-s;
s=clock();
test("d2.in",1000000);
e=clock();
cout<<"\n10^6 integer:"<<e-s;
s=clock();
test("d3.in",10000000);
e=clock();
cout<<"\n10^7 integer:"<<e-s;
return 0;
}