OI 中各种输入方式的速度比较(C++,大量实测数据,附图表)

测试信息

本次共测试了以下几种输入方式的速度:

  • scanf
  • cin
  • 快读
  • 位运算快读
  • fread() + 位运算快读
  • 关闭同步流的 cin
  • 开启 tie 绑定并关闭同步流的 cin

每组测试各输入方式均使用相同数据,为随机生成的 1000000 1000000 10000001E6) 个整数,范围在 [ − ( 2 31 − 1 ) , 2 31 − 1 ] [-(2^{31}-1),2^{31}-1] [(2311),2311](即 int 范围)。

所用配置为 12th Gen Intel(R) Core(TM) i5-12400 2.50 GHz,操作系统为 Windows 11


所用代码:

数据生成

#include<cstdio>
#include<cstdlib>
#include<ctime>
using namespace std;

const int N=1e6;

inline int large_rand()
{
	bool op=rand()&1;
	long long huge_rand=rand()+(rand()<<15)+(rand()<<30);
	return op?-(huge_rand%(1ll<<32)):huge_rand%(1ll<<32);
}
int main()
{
	freopen("input.in","w",stdout);
	srand(time(NULL));
	for(int i=1;i<=N;i++)
		printf("%d ",large_rand());
	fclose(stdout);
	freopen("log.txt","a+",stdout);
	printf("Succeeded generated data.\n");
	fclose(stdout);
	return 0;
}

计时等部分

#include<cstdio>
#include<windows.h>
using namespace std;

const int N=1e6;

int main()
{
	freopen("input.in","r",stdin);
	freopen("log.txt","a+",stdout);
	
    LARGE_INTEGER StartingTime,EndingTime,ElapsedMicroseconds;
    LARGE_INTEGER Frequency;

    QueryPerformanceFrequency(&Frequency);
    QueryPerformanceCounter(&StartingTime);
	
	...

    QueryPerformanceCounter(&EndingTime);
    printf("xxx: %.4f ms\n",1000*((double)EndingTime.QuadPart-StartingTime.QuadPart)/Frequency.QuadPart);
    
    fclose(stdin);
	return 0; 
}

scanf

	int x;
    for(int i=1;i<=N;i++)
    	scanf("%d",&x);

cin

	int x;
    for(int i=1;i<=N;i++)
    	cin>>x;

快读

int read()
{
	int x=0,w=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*w;
}
...
	int x;
    for(int i=1;i<=N;i++)
    	x=read();

位运算优化快读

int read()
{
	int x=0,w=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^'0');ch=getchar();}
	return x*w;
}
...
	int x;
    for(int i=1;i<=N;i++)
    	x=read();

fread() + 位运算优化快读

const int MAXSIZE=1<<20;
char buf[MAXSIZE],*p1,*p2;
#define gc()                                                               \
  (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) \
       ? EOF                                                               \
       : *p1++)

int rd()
{
	int x=0,f=1;
	char c=gc();
	while(!isdigit(c))
	{
		if(c=='-') f=-1;
		c=gc();
	}
	while (isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=gc();
	return x*f;
}
...
	int x;
    for(int i=1;i<=N;i++)
    	x=rd();

关闭同步流的 cin

	std::ios::sync_with_stdio(false);
	int x;
    for(int i=1;i<=N;i++)
    	cin>>x;

开启 tie 绑定并关闭同步流的 cin

	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int x;
    for(int i=1;i<=N;i++)
    	cin>>x;

测试结果

log.txt 输出信息

#1
Succeeded generated data.
scanf: 425.8919 ms
cin: 1128.8144 ms
read: 204.4151 ms
read+: 208.4703 ms
read++: 35.9450 ms
cin ios: 127.2909 ms
cin tie ios: 129.4965 ms

#2
Succeeded generated data.
scanf: 442.5600 ms
cin: 1260.4899 ms
read: 210.9504 ms
read+: 209.6824 ms
read++: 44.4940 ms
cin ios: 127.7128 ms
cin tie ios: 144.6090 ms

#3
Succeeded generated data.
scanf: 438.7641 ms
cin: 1166.4752 ms
read: 213.7551 ms
read+: 230.8293 ms
read++: 44.0495 ms
cin ios: 135.8041 ms
cin tie ios: 137.5192 ms

表格统计

表格统计

图表统计

统计表

根据统计,在百万较长整数(大多是九到十位数)的测试数据下,fread()等优化后的超级快读速度极快,快读及位运算优化快读的效率相似,iostie 优化后的 cin 效率较高,scanf 稍慢,裸 cin 的效率极低

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值