输入速度测试
Transparent · · 个人记录
上次测试用的学校的旧电脑,硬盘成瓶颈了,今天重新测一次。
基本信息
测试环境
懒得搞 Linux,直接用的 Windows。
Windows 11 22H2 22621.2428
AMD Ryzen 7 4800H with Radeon Graphics 2.90 GHz
Dual Channel DDR4-3200 SDRAM (22-22-22-52 CR1)
硬盘跑分在下面(Ram Disk,随机有点寄,但是应该问题不大),几乎可以避免硬盘读写速度成为瓶颈。
运行的程序的时候,CPU 有负载的核心都把频率控制在
缓存全部开的
读取一般按行读取。
测试结果
参考数据:某些毒瘤题一个输入文件大小在
随机字母
文件包含随机生成的字母和换行符,每 \n,其余位置为随机产生的小写英文字母,文件大小
测试结果(按速度从快到慢排序):
fread:用时5.683 \text{s} ,平均速度720.746 \text{MiB/s} 。ifstream:用时15.995 \text{s} ,平均速度256.08 \text{MiB/s} 。cin+ios::sync_with_stdio(false):用时16.124 \text{s} ,平均速度254.031 \text{MiB/s} 。fgets:用时16.985 \text{s} ,平均速度241.154 \text{MiB/s} 。
getchar:用时68.351 \text{s} ,平均速度59.926 \text{MiB/s} 。scanf:用时88.052 \text{s} ,平均速度46.518 \text{MiB/s} 。cin:用时216.278 \text{s} ,平均速度18.9386 \text{MiB/s} 。
随机数字
文件包含随机生成但是长度固定的正整数,每 \n,包含
测试结果(按速度从快到慢排序):
fread:用时6.647 \text{s} ,平均速度616.218 \text{MiB/s} 。ifstream:用时37.23 \text{s} ,平均速度110.019 \text{MiB/s} 。cin+ios::sync_with_stdio(false):用时39.26 \text{s} ,平均速度104.33 \text{MiB/s} 。
getchar:用时73.058 \text{s} ,平均速度56.065 \text{MiB/s} 。scanf:用时223.737 \text{s} ,平均速度18.3072 \text{MiB/s} 。cin:用时414.642 \text{s} ,平均速度9.8784 \text{MiB/s} 。
字符串采用类似以下的方式读入,求和用于检验结果:
FILE *f=fopen("rand_letter.txt","r");
while(true) {
memset(buf,0,sizeof(buf));
int res=fread(buf,1,SIZE,f);
if(!buf[0]) break;
for(int i=0;i<res;++i) if(buf[i]>='a'&&buf[i]<='z') sum+=buf[i];
}
clock_t end=clock();
double dt=double(end)/double(CLOCKS_PER_SEC);
cout<<sum<<endl;
cout<<"time:"<<dt<<"s"<<" speed:"<<4096./dt<<"MiB/s"<<endl;
快读采用以下实现,其中测试 getchar 时直接注释掉新定义的 getchar 函数。
namespace Read {
char *cur=buf+SIZE,*ed=buf+SIZE;
inline char getchar() {
if(cur==ed) fread(buf,1,SIZE,f),cur=buf;
return *(cur++);
}
template<typename T> inline void read(T &x) {
char ch=getchar(); bool flag=false;
while(ch<'0'||ch>'9') {
if(ch=='-') flag=true;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
x=flag?-x:x;
}
}
整数读取采用类似以下的实现,求和用于检验结果:
int T=13421773*32,tmp;
while(T--) {
cin>>tmp; sum+=tmp;
}
clock_t end=clock();
double dt=double(end)/double(CLOCKS_PER_SEC);
cout<<sum<<endl;
cout<<"time:"<<dt<<"s"<<" speed:"<<4096./dt<<"MiB/s"<<endl;