跳到正文

个人记录

【测评】各输入方式的测评比较

liangbob约 8 分钟
原文
文章信息与版本

文章信息

发布
2023年12月16日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 8 分钟
获赞
1

版本

  1. v1当前

各输入方式的测评比较

测评内容

测评以下五种读入方式:

  • scanf

  • cin

  • cin 关闭流同步 + tie(0)

  • getchar() 快读,代码如下:

C++
inline int read()
{
    int x = 0,f = 1;
    char c = getchar();
    while(c <'0'|| c >'9'){if(c == '-') f = -1;c = getchar();}
    while(c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0',c = getchar();
    return x * f;
}

测评环境

CPU:Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz 3.60 GHz

系统:Windows 10 专业版 22H2.

编译器:TDM-GCC 4.9.2 64-bit Release

编译参数:-std=c++11 -Wall -Wextra -O2

测评过程

我们使用 C++ 程序输出一亿个数:

C++
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	ios::sync_with_stdio(0);
	cout.tie(0);
	freopen("test.in", "w", stdout);
	for(int i = 0;i < 100000000;i++) cout << i << " "; 
} 

接着编写 5 个待测程序:

  • 程序 1:scanf
C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	freopen("test.in", "r", stdin);
	while(~scanf("%d", &n));
	return 0;	
} 
  • 程序 2:cin
C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	freopen("test.in", "r", stdin);
	while(cin >> n);
	return 0;	
} 
  • 程序 3:cin 关闭流同步 + tie(0)
C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	freopen("test.in", "r", stdin);
	while(cin >> n);
	return 0; 
} 
  • 程序 4:getchar() 快读
C++
#include <bits/stdc++.h>
#define IL inline
using namespace std;

IL int read()
{
    int x = 0,f = 1;
    char c = getchar();
    while(c <'0'|| c >'9'){if(c == '-') f = -1;c = getchar();}
    while(c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0',c = getchar();
    return x * f;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	freopen("test.in", "r", stdin);
	int n;
	for(int i = 1;i <= 100000000;i++)
	{
		n = read();
	}
	return 0;	
} 
  • 程序 5:fread 快读
C++
#include <cstdio>

using namespace std;
// orz laofudasuan
// modified

namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	template <class I>
	inline void gi (I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
	}
	// print a signed integer
	template <class I>
	inline void print (I x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: gi;
using io :: putc;
using io :: print;

int n;

int main () {
	freopen("test.in", "r", stdin);
	for(int i = 1;i <= 100000000;i++) gi(n);
    return 0;
}

最后,编写测评程序。这里使用了较为精确的 chrono 实现纳秒级计时。

C++
#include <bits/stdc++.h>
#include <chrono>
#define endl '\n'
#define IL inline
using namespace std;
//1: scanf
//2: cin
//3: optmized cin
//4: getchar-fastio
//5: fread-fastio 


int main()
{
	freopen("STestResult.txt", "w", stderr); 
	for(int i = 1;i <= 5;i++)
	{
		string t = to_string(i);
		cout << "Start To Test Program " << i << endl; 
		auto t1 = std::chrono::system_clock::now();
    	system(t.c_str());
    	auto t2 = std::chrono::system_clock::now();
    	cout << "End To Test Program " << i << endl; 
    	double p = std::chrono::duration<double,std::nano>(t2-t1).count();
    	cerr << "Program " << i << " Used " << fixed << setprecision(6) << p / 1000000000.0;
    	cerr << " s to finish" << endl;
	}
    return 0;
}

测评结果

我们运行了三次测评程序,得到结果如下:

https://www.luogu.com.cn/paste/hx1lahrb

结论:

fread<getchar<优化 cin<scanf<cin\text{fread} < \text{getchar} < \text{优化 cin} < \text{scanf} < \text{cin}

原文
存档评论5 条抓取于