C++ 每日一题7:速度

每日一题7:速度

题目描述

信息学比赛中有时输入数据很大,数据文件的读取方式和效率就要重视了。一般地,使用scanf、printf比使用cin、cout速度快。请根据以下测试表,重新设计方案,对各种方式的文件进行速度测试。可适当在输出部分提示测试方案以及结果。

i7+12G+固盘+devcpp5.6.1文件速度测试对比表
读入方式10^5个整数时间(ms)10^6个整数时间(ms)10^7个整数时间(ms)
ifstream+fin161031001
FILE*+scanf553453366
freopen+cin674404378
freopen+cin+sync_with_stdio(false)91031024
freopen+scanf323183112
i2+2G+普通硬盘+devcpp4.9.9.2文件速度测试对比表
读入方式10^5个整数时间(ms)10^6个整数时间(ms)10^7个整数时间(ms)
ifstream+fin659826148
FILE*+scanf536924829
freopen+cin727645062738
freopen+cin+sync_with_stdio(false)736746533
freopen+scanf555104579

输入格式

无。

输出格式

可自行定义。

例如:"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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值