- 2022tysc1461 的博客
C++各种读入输出的方式的效率比较
- @ 2023-1-20 16:35:53
-1.题外话
最近几天都在暴力卡 [WC2022] 秃子酋长。目前最好的成绩为 #14 测试点 。(求各位大佬帮我卡一下 #13 和 #16,它们的数据比较弱)
0.环境与配置(可省略)
(1)环境
Windows 用的是版本为 22H2 的 Windows 11 专业版。
Linux 用的是 CCF 发布的 NOI Linux 2.0。(使用 Virtual Box 虚拟机,分配内存为 GB)
(2)配置
处理器为 AMD Ryzen 7 5800H with Radeon Graphics。 内存(RAM)为 GB。
(3)测试数据
共有 组测试数据,其中第 组数据命名为 test #i.txt。每组测试数据都有一个 ()和 个用 mt19937 随机生成的正整数(但是下面的代码都会兼容负数),数和数之间用 \n 换行符隔开。
具体的生成测试数据代码如下:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7;
string num, file;
int main(){
mt19937 myrand(time(0));
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "w", stdout);
cout << N << '\n';
for (int i=1; i<=N; i++) cout << myrand() << '\n';
fclose(stdout);
}
return 0;
}
每个测试数据都有 MB。
(4)编译
Windows 使用 Dev-Cpp 5.11 自带的 TDM-GCC 4.9.2 64-bit Release。且 Optimization level (-Ox) 的值设为了 Med (相当于 -O2),Language standard(-std) 的值设为了 ISO C++11(相当于 -std=c++11)。
Linux 使用的是 NOI Linux 自带的 G++ 9.3.0。编译命令为 g++ -o xxx.exe xxx.cpp -O2 -std=c++11。(其中 xxx 表示文件名)
1.读入
(1)cin
cin 大概是大家用的最多的一个了吧 (我瞎猜的),它不仅简单,而且还有一个极为显著的忧点——慢。那么它究竟有多慢呢,让我们拭目以待:
这是 cin_1.cpp 的代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
int main(){
freopen("cin_1.txt", "w", stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
start_time = clock();
cin >> n;
for (int j=1; j<=n; j++) cin >> a[i][j];
end_time = clock();
fclose(stdin);
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: cin_1 used time: %.5f s.\n", i, run_time);
all_time += run_time;
}
printf("\n");
printf("All: cin_1 used time: %.5f s.\n", all_time);
printf("Average: cin_1 used time: %.5f s.", all_time/5);
return 0;
}
运行后在 Linux 里 cin_1.txt 文件的内容是这样的:
#1: cin_1 used time: 6.71428 s.
#2: cin_1 used time: 6.74294 s.
#3: cin_1 used time: 6.80542 s.
#4: cin_1 used time: 6.74385 s.
#5: cin_1 used time: 6.89626 s.
All: cin_1 used time: 33.90276 s.
Average: cin_1 used time: 6.78055 s.
可以看到,cin 读取 的平均用时约为 。比我想象的快了一些。
在 Windows 里的 cin_1.txt 的内容却是这样的:
#1: cin_1 used time: 5.46600 s.
#2: cin_1 used time: 5.52100 s.
#3: cin_1 used time: 5.51700 s.
#4: cin_1 used time: 5.48800 s.
#5: cin_1 used time: 5.56100 s.
All: cin_1 used time: 27.55300 s.
Average: cin_1 used time: 5.51060 s.
而在 Windows 中,cin 的用时降到了 ,比 Linux 约快了 !
(2)scanf
scanf 是C语言里的读入函数,使用比 cin 较为麻烦(但是在读取某些神奇数据的时候却方便很多,比如读取 hh:mm:ss 这样中间有冒号的数据)。它的特点就是比cin快,让我们来看看它的成绩吧。
这时 scanf.cpp 的代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
int main(){
freopen("scanf.txt", "w", stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
start_time = clock();
scanf("%lld", &n);
for (int j=1; j<=n; j++) scanf("%lld", &a[i][j]);
end_time = clock();
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: scanf used time: %.5f s.\n", i, run_time);
all_time += run_time;
}
printf("\n");
printf("All: scanf used time: %.5f s.\n", all_time);
printf("Average: scanf used time: %.5f s.", all_time/5);
return 0;
}
编译并运行后,在 Linux 下的 scanf.txt 文件内容如下:
#1: scanf used time: 0.75964 s.
#2: scanf used time: 0.76995 s.
#3: scanf used time: 0.76777 s.
#4: scanf used time: 0.76498 s.
#5: scanf used time: 0.77012 s.
All: scanf used time: 3.83245 s.
Average: scanf used time: 0.76649 s.
可以看到 scanf 还是非常快的, 根本不在话下。用时比 cin 快了 !
但是在 Windows 下,scanf.txt 的文件内容如下:
#1: scanf used time: 3.88800 s.
#2: scanf used time: 3.81300 s.
#3: scanf used time: 3.92200 s.
#4: scanf used time: 4.00200 s.
#5: scanf used time: 3.97700 s.
All: scanf used time: 19.60200 s.
Average: scanf used time: 3.92040 s.
这整整比 Linux 下的慢了约 !难道 Windows 只适合用 cin?望各位大佬解答一下。
(3)cin+解除绑定
众所周知,有那么两条语句可以加快程序的速度:
cin.tie(0);
cout.tie(0);
看了解释后我们有一个疑问,如果不用 cout,那么这条语句还有用吗?于是,cin_2.cpp 的代码便横空出世了:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
int main(){
cin.tie(0);
freopen("cin_2.txt", "w", stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
start_time = clock();
cin >> n;
for (int j=1; j<=n; j++) cin >> a[i][j];
end_time = clock();
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: cin_2 used time: %.5f s.\n", i, run_time);
all_time += run_time;
}
printf("\n");
printf("All: cin_2 used time: %.5f s.\n", all_time);
printf("Average: cin_2 used time: %.5f s.", all_time/5);
return 0;
}
Linux下的 cin_2.txt 文件内容:
#1: cin_2 used time: 6.45239 s.
#2: cin_2 used time: 6.51081 s.
#3: cin_2 used time: 6.51259 s.
#4: cin_2 used time: 6.53115 s.
#5: cin_2 used time: 6.49747 s.
All: cin_2 used time: 32.50440 s.
Average: cin_2 used time: 6.50088 s.
唔,也不能说没有优化吧,只能说是优化有点少,只优化了约 。还需努力呀!
看看 Windows 表现得怎么样吧:
#1: cin_2 used time: 5.48800 s.
#2: cin_2 used time: 5.48900 s.
#3: cin_2 used time: 5.49600 s.
#4: cin_2 used time: 5.44400 s.
#5: cin_2 used time: 5.38600 s.
All: cin_2 used time: 27.30300 s.
Average: cin_2 used time: 5.46060 s.
Windows 还更差,只优化了约 ,才 Linux 优化程度得 !
(4)cin+解除绑定+关闭同步
众所周知,还有另外一调语句可以让 cin 更快,他就是:
ios::sync_with_stdio(false);
它的作用就是解除 stream 和 stdio 之间的关系,让它们不再兼容。
**注意,再关闭同步后,不能再使用 stdio 里的读入输出函数!**除非你是只使用 cin 和 printf 或 putchar 等。
那么测试代码来了:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
freopen("cin_3.txt", "w", stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
start_time = clock();
cin >> n;
for (int j=1; j<=n; j++) cin >> a[i][j];
end_time = clock();
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: cin_3 used time: %.5f s.\n", i, run_time);
all_time += run_time;
}
printf("\n");
printf("All: cin_3 used time: %.5f s.\n", all_time);
printf("Average: cin_3 used time: %.5f s.", all_time/5);
return 0;
}
Linux 下的战绩:
#1: cin_3 used time: 0.82049 s.
#2: cin_3 used time: 0.82190 s.
#3: cin_3 used time: 0.82166 s.
#4: cin_3 used time: 0.82499 s.
#5: cin_3 used time: 0.82926 s.
All: cin_3 used time: 4.11832 s.
Average: cin_3 used time: 0.82366 s.
这个速度比一开始的 cin 快了约 ,极大的优化!
stdio 对 stream 说:我碍你!我碍你呀!
在 Windows 下的战绩:
#1: cin_3 used time: 1.22700 s.
#2: cin_3 used time: 1.24200 s.
#3: cin_3 used time: 1.18000 s.
#4: cin_3 used time: 1.15300 s.
#5: cin_3 used time: 1.16900 s.
All: cin_3 used time: 5.97100 s.
Average: cin_3 used time: 1.19420 s.
Windows竟然比Linux慢了 !但是它却比在Windows下的 scanf 快了 ,真是“比上不足,比下有余”。
(5)快读(非 void 版)
众所周知,有一种方法叫做“快读”。它有着极高的效率,让某些骗分党(比如说我)获得多 分甚至 分的优秀成绩。
非 void 版快读模板:
inline long long fast_read_1(){
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = getchar())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = getchar()));
return x*f;
}
那么测试代码即为:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
inline long long fast_read_1(){
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = getchar())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = getchar()));
return x*f;
}
int main(){
freopen("fast_read_1.txt", "w", stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
start_time = clock();
n = fast_read_1();
for (int j=1; j<=n; j++) a[i][j] = fast_read_1();
end_time = clock();
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: fast_read_1 used time: %.5f s.\n", i, run_time);
all_time += run_time;
}
printf("\n");
printf("All: fast_read_1 used time: %.5f s.\n", all_time);
printf("Average: fast_read_1 used time: %.5f s.", all_time/5);
return 0;
}
看一下速度吧,它在Linux下的输出如下:
#1: fast_read_1 used time: 0.82733 s.
#2: fast_read_1 used time: 0.83555 s.
#3: fast_read_1 used time: 0.84672 s.
#4: fast_read_1 used time: 0.83771 s.
#5: fast_read_1 used time: 0.83938 s.
All: fast_read_1 used time: 4.18668 s.
Average: fast_read_1 used time: 0.83734 s.
这TM还没有scanf快呢,什么鬼!
那么在Windows呢:
#1: fast_read_1 used time: 1.33500 s.
#2: fast_read_1 used time: 1.33600 s.
#3: fast_read_1 used time: 1.32000 s.
#4: fast_read_1 used time: 1.34400 s.
#5: fast_read_1 used time: 1.33200 s.
All: fast_read_1 used time: 6.66700 s.
Average: fast_read_1 used time: 1.33340 s.
看起来很差,但是之前的Windows也没有好到哪里去。
现在知道为什么评测姬要用Linux了吧。
(6)快读(void 版)
众所周知,还有一种快读,它和上一种快读写法差不多,但是使用了左值引用(其实就是 & 的高级说法)来替代返回值,效率进一步提升。
代码如下(这里不用 isdigit 更快):
inline void fast_read_2(long long& x){
x = 0;
long long f = 1;
char ch;
while ((ch = getchar()) < 48){
if (ch == '-') f = -1;
}
do{
x = (x << 1)+(x << 3)+(ch ^ 48);
}while ((ch = getchar()) > 47);
}
测试代码如下:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
inline void fast_read_2(long long& x){
x = 0;
long long f = 1;
char ch;
while ((ch = getchar()) < 48){
if (ch == '-') f = -1;
}
do{
x = (x << 1)+(x << 3)+(ch ^ 48);
}while ((ch = getchar()) > 47);
}
int main(){
freopen("fast_read_2.txt", "w", stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
start_time = clock();
fast_read_2(n);
for (int j=1; j<=n; j++) fast_read_2(a[i][j]);
end_time = clock();
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: fast_read_2 used time: %.5f s.\n", i, run_time);
all_time += run_time;
}
printf("\n");
printf("All: fast_read_2 used time: %.5f s.\n", all_time);
printf("Average: fast_read_2 used time: %.5f s.", all_time / 5);
return 0;
}
在Linux下的输出:
#1: fast_read_2 used time: 0.77677 s.
#2: fast_read_2 used time: 0.77890 s.
#3: fast_read_2 used time: 0.77672 s.
#4: fast_read_2 used time: 0.78222 s.
#5: fast_read_2 used time: 0.77718 s.
All: fast_read_2 used time: 3.89179 s.
Average: fast_read_2 used time: 0.77836 s.
在Windows下的输出:
#1: fast_read_2 used time: 1.32400 s.
#2: fast_read_2 used time: 1.36300 s.
#3: fast_read_2 used time: 1.36500 s.
#4: fast_read_2 used time: 1.38100 s.
#5: fast_read_2 used time: 1.36300 s.
All: fast_read_2 used time: 6.79600 s.
Average: fast_read_2 used time: 1.35920 s.
在Windows下被“cin+解除绑定+关闭同步”暴打,在Linux下被 scanf 血虐。快读,还有希望吗?
还有!!!
(7)快读+fread(无 void 版)
下面是 fread 函数的声明:
std::size_t fread(void* buffer, std::size_t size, std::size_t count,std::FILE* stream);
示例:
fread(buf, size, count, stdin);
表示从 stdin 文件流中读入 count 个大小为 size byte 的数据块到 buf 中。
现在肯定有人问了:它看起来很厉害,但是又什么用呢?
那用处可大大滴有!!!
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
搞完上面的宏定义后,调用 gc() 即可获得和 getchar() 一样的效果。
那么快读+fread 的代码如下:
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline long long read() {
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
return x*f;
}
此处的 read() 和其它快读的 read() 一样。
那么测试程序如下:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline long long read() {
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
return x*f;
}
int main(){
freopen("fread_1.txt", "w", stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
start_time = clock();
n = read();
for (int j=1; j<=n; j++) a[i][j] = read();
end_time = clock();
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: fread_1 used time: %.5f s.\n", i, run_time);
all_time += run_time;
}
printf("\n");
printf("All: fread_1 used time: %.5f s.\n", all_time);
printf("Average: fread_1 used time: %.5f s.", all_time/5);
return 0;
}
Linux下的战绩:
#1: fread_1 used time: 0.11458 s.
#2: fread_1 used time: 0.11322 s.
#3: fread_1 used time: 0.11280 s.
#4: fread_1 used time: 0.11276 s.
#5: fread_1 used time: 0.11284 s.
All: fread_1 used time: 0.56620 s.
Average: fread_1 used time: 0.11324 s.
Windows下的战绩:
#1: fread_1 used time: 0.19500 s.
#2: fread_1 used time: 0.19000 s.
#3: fread_1 used time: 0.19200 s.
#4: fread_1 used time: 0.19100 s.
#5: fread_1 used time: 0.19100 s.
All: fread_1 used time: 0.95900 s.
Average: fread_1 used time: 0.19180 s.
这个0.19180真的不是我造的,是它输出来的。
什么叫做神?这个就是神!!!
(8)快读+fread(void 版)
这个就直接放代码了吧,处理过程上面都说过了。
模板代码:
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline void read(long long& x) {
long long f = 1;
x = 0;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
x *= f;
}
测试代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline void read(long long& x) {
long long f = 1;
x = 0;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
x *= f;
}
int main(){
freopen("fread_2.txt", "w", stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
start_time = clock();
read(n);
for (int j=1; j<=n; j++) read(a[i][j]);
end_time = clock();
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: fread_2 used time: %.5f s.\n", i, run_time);
all_time += run_time;
}
printf("\n");
printf("All: fread_2 used time: %.5f s.\n", all_time);
printf("Average: fread_2 used time: %.5f s.", all_time/5);
return 0;
}
Linux下输出数据:
#1: fread_2 used time: 0.12711 s.
#2: fread_2 used time: 0.12655 s.
#3: fread_2 used time: 0.12543 s.
#4: fread_2 used time: 0.12460 s.
#5: fread_2 used time: 0.12474 s.
All: fread_2 used time: 0.62843 s.
Average: fread_2 used time: 0.12569 s.
Windows下输出数据:
#1: fread_2 used time: 0.21100 s.
#2: fread_2 used time: 0.20100 s.
#3: fread_2 used time: 0.20500 s.
#4: fread_2 used time: 0.20100 s.
#5: fread_2 used time: 0.20000 s.
All: fread_2 used time: 1.01800 s.
Average: fread_2 used time: 0.20360 s.
还是慢了一点呀,不过已经特别厉害了。
(9)读入速度比较(数据数量级为的平均用时,保留 位小数):
| 项目 | Linux(s) | Windows用时(s) |
|---|---|---|
| cin | ||
| scanf | ||
| cin+解除绑定 | ||
| cin+解除绑定+关闭同步 | ||
快读(无 void 版) |
||
快读(void 版) |
||
快读+fread(无 void 版) |
||
快读+fread(void 版) |
2.输出
(1)cout
第一个当然还是我们的老朋友 cout 呀!
测试代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline long long read() {
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
return x*f;
}
int main(){
freopen("cout_1.txt", "w", stdout);
fclose(stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
freopen("cout_1 out.txt", "w", stdout);
n = read();
for (int j=1; j<=n; j++) a[i][j] = read();
fclose(stdin);
start_time = clock();
cout << n << '\n';
for (int j=1; j<=n; j++) cout << a[i][j] << '\n';
end_time = clock();
fclose(stdout);
freopen("cout_1.txt", "a", stdout);
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: cout_1 used time: %.5f s.\n", i, run_time);
all_time += run_time;
fclose(stdout);
}
freopen("cout_1.txt", "a", stdout);
printf("\n");
printf("All: cout_1 used time: %.5f s.\n", all_time);
printf("Average: cout_1 used time: %.5f s.", all_time/5);
return 0;
}
Linux下战绩:
#1: cout_1 used time: 1.34336 s.
#2: cout_1 used time: 1.36187 s.
#3: cout_1 used time: 1.35559 s.
#4: cout_1 used time: 1.35876 s.
#5: cout_1 used time: 1.36515 s.
All: cout_1 used time: 6.78473 s.
Average: cout_1 used time: 1.35695 s.
Windows下战绩:
#1: cout_1 used time: 0.75800 s.
#2: cout_1 used time: 0.76600 s.
#3: cout_1 used time: 0.75400 s.
#4: cout_1 used time: 0.76700 s.
#5: cout_1 used time: 0.79900 s.
All: cout_1 used time: 3.84400 s.
Average: cout_1 used time: 0.76880 s.
这速度,cin 表示自己当年也快要顶的上自己兄弟……的 了。
(2)printf
printf 也是老兄弟了,我就不介绍了吧。
测试代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline long long read() {
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
return x*f;
}
int main(){
freopen("printf.txt", "w", stdout);
fclose(stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
freopen("printf out.txt", "w", stdout);
n = read();
for (int j=1; j<=n; j++) a[i][j] = read();
fclose(stdin);
start_time = clock();
printf("%d\n", n);
for (int j=1; j<=n; j++) printf("%d\n", a[i][j]);
end_time = clock();
fclose(stdout);
freopen("printf.txt", "a", stdout);
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: printf used time: %.5f s.\n", i, run_time);
all_time += run_time;
fclose(stdout);
}
freopen("printf.txt", "a", stdout);
printf("\n");
printf("All: printf used time: %.5f s.\n", all_time);
printf("Average: printf used time: %.5f s.", all_time/5);
return 0;
}
Linux下战绩:
#1: printf used time: 1.07143 s.
#2: printf used time: 1.05532 s.
#3: printf used time: 1.10013 s.
#4: printf used time: 1.08045 s.
#5: printf used time: 1.09006 s.
All: printf used time: 5.39739 s.
Average: printf used time: 1.07948 s.
Windows下战绩:
#1: printf used time: 11.50800 s.
#2: printf used time: 11.68000 s.
#3: printf used time: 11.66400 s.
#4: printf used time: 11.57900 s.
#5: printf used time: 11.59200 s.
All: printf used time: 58.02300 s.
Average: printf used time: 11.60460 s.
阿哲,Windows下这么惨的吗? 都破了低效率记录了!
(3)cout+解除绑定
其实就是加上这句话:
cout.tie(0);
测试代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline long long read() {
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
return x*f;
}
int main(){
cout.tie(0);
freopen("cout_2.txt", "w", stdout);
fclose(stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
freopen("cout_2 out.txt", "w", stdout);
n = read();
for (int j=1; j<=n; j++) a[i][j] = read();
fclose(stdin);
start_time = clock();
cout << n << '\n';
for (int j=1; j<=n; j++) cout << a[i][j] << '\n';
end_time = clock();
fclose(stdout);
freopen("cout_2.txt", "a", stdout);
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: cout_2 used time: %.5f s.\n", i, run_time);
all_time += run_time;
fclose(stdout);
}
freopen("cout_2.txt", "a", stdout);
printf("\n");
printf("All: cout_2 used time: %.5f s.\n", all_time);
printf("Average: cout_2 used time: %.5f s.", all_time/5);
return 0;
}
Linux下战绩:
#1: cout_2 used time: 1.34469 s.
#2: cout_2 used time: 1.34244 s.
#3: cout_2 used time: 1.33422 s.
#4: cout_2 used time: 1.34204 s.
#5: cout_2 used time: 1.32639 s.
All: cout_2 used time: 6.68978 s.
Average: cout_2 used time: 1.33796 s.
Windows下的战绩:
#1: cout_2 used time: 0.75000 s.
#2: cout_2 used time: 0.76700 s.
#3: cout_2 used time: 0.74700 s.
#4: cout_2 used time: 0.76300 s.
#5: cout_2 used time: 0.75000 s.
All: cout_2 used time: 3.77700 s.
Average: cout_2 used time: 0.75540 s.
还是Windows更胜一筹呀,以 的巨大优势胜利。
(4)cout+解除绑定+关闭同步
大家都懂的吧,前面提到过。
测试代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline long long read() {
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
return x*f;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
freopen("cout_3.txt", "w", stdout);
fclose(stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
freopen("cout_3 out.txt", "w", stdout);
n = read();
for (int j=1; j<=n; j++) a[i][j] = read();
fclose(stdin);
start_time = clock();
cout << n << '\n';
for (int j=1; j<=n; j++) cout << a[i][j] << '\n';
end_time = clock();
fclose(stdout);
freopen("cout_3.txt", "a", stdout);
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: cout_3 used time: %.5f s.\n", i, run_time);
all_time += run_time;
fclose(stdout);
}
freopen("cout_3.txt", "a", stdout);
printf("\n");
printf("All: cout_3 used time: %.5f s.\n", all_time);
printf("Average: cout_3 used time: %.5f s.", all_time/5);
return 0;
}
Linux下的战绩:
#1: cout_3 used time: 1.30759 s.
#2: cout_3 used time: 1.29225 s.
#3: cout_3 used time: 1.29146 s.
#4: cout_3 used time: 1.29905 s.
#5: cout_3 used time: 1.30174 s.
All: cout_3 used time: 6.49209 s.
Average: cout_3 used time: 1.29842 s.
Windows下的战绩:
#1: cout_3 used time: 1.39700 s.
#2: cout_3 used time: 1.45500 s.
#3: cout_3 used time: 1.45100 s.
#4: cout_3 used time: 1.40100 s.
#5: cout_3 used time: 1.40800 s.
All: cout_3 used time: 7.11200 s.
Average: cout_3 used time: 1.42240 s.
好家伙,这就是完完全全的负优化呀。避坑了。
(5)快写(递归版)
快写的模板程序如下:
inline void write(long long x){
if (x < 0){
putchar('-');
x = -x;
}
int tmp = x/10;
if (x > 9) write(tmp);
putchar(x-(tmp<<1)-(tmp<<3)+'0');
}
注意到下面的 x-(tmp<<1)-(tmp<<3),其实就相当于 x % 10。可以自己尝试李姐。
测试代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline long long read() {
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
return x*f;
}
inline void write(long long x){
if (x < 0){
putchar('-');
x = -x;
}
int tmp = x/10;
if (x > 9) write(tmp);
putchar(x-(tmp<<1)-(tmp<<3)+'0');
}
int main(){
freopen("fast_write_1.txt", "w", stdout);
fclose(stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
freopen("fast_write_1 out.txt", "w", stdout);
n = read();
for (int j=1; j<=n; j++) a[i][j] = read();
fclose(stdin);
start_time = clock();
write(n);
putchar('\n');
for (int j=1; j<=n; j++){
write(a[i][j]);
putchar('\n');
}
end_time = clock();
fclose(stdout);
freopen("fast_write_1.txt", "a", stdout);
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: fast_write_1 used time: %.5f s.\n", i, run_time);
all_time += run_time;
fclose(stdout);
}
freopen("fast_write_1.txt", "a", stdout);
printf("\n");
printf("All: fast_write_1 used time: %.5f s.\n", all_time);
printf("Average: fast_write_1 used time: %.5f s.", all_time/5);
return 0;
}
Linux下的战绩:
#1: fast_write_1 used time: 0.91420 s.
#2: fast_write_1 used time: 0.91349 s.
#3: fast_write_1 used time: 0.91707 s.
#4: fast_write_1 used time: 0.92064 s.
#5: fast_write_1 used time: 0.91327 s.
All: fast_write_1 used time: 4.57867 s.
Average: fast_write_1 used time: 0.91573 s.
Windows下的战绩:
#1: fast_read_1 used time: 1.33500 s.
#2: fast_read_1 used time: 1.33600 s.
#3: fast_read_1 used time: 1.32000 s.
#4: fast_read_1 used time: 1.34400 s.
#5: fast_read_1 used time: 1.33200 s.
All: fast_read_1 used time: 6.66700 s.
Average: fast_read_1 used time: 1.33340 s.
总算好了些呀,不像快读或者上面那种了。(滑稽)
(7)快写(非递归)
那么快写还有一个版本,是非递归的。
考虑一个栈。首先放入一个这个数的个位,然后再放十位……放到最后一位。然后再从栈顶取数并输出,就可以输出一个数了。
以为STL的 stack 常数太高,所以我们手写栈。
而且 short 比较慢,于是我们用 int 来。
inline void write(long long x){
int st[20], p = 0, tmp;
while (x){
tmp = x/10;
st[++p] = x-(tmp<<1)-(tmp<<3);
x = tmp;
}
while (p > 0){
putchar(st[p--]+'0');
}
}
测试代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t start_time, end_time;
double run_time, all_time;
string num, file;
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline long long read() {
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
return x*f;
}
inline void write(long long x){
int st[20], p = 0, tmp;
while (x){
tmp = x/10;
st[++p] = x-(tmp<<1)-(tmp<<3);
x = tmp;
}
while (p > 0){
putchar(st[p--]+'0');
}
}
int main(){
freopen("fast_write_2.txt", "w", stdout);
fclose(stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
freopen("fast_write_2 out.txt", "w", stdout);
n = read();
for (int j=1; j<=n; j++) a[i][j] = read();
fclose(stdin);
start_time = clock();
write(n);
putchar('\n');
for (int j=1; j<=n; j++){
write(a[i][j]);
putchar('\n');
}
end_time = clock();
fclose(stdout);
freopen("fast_write_2.txt", "a", stdout);
run_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
printf("#%d: fast_write_2 used time: %.5f s.\n", i, run_time);
all_time += run_time;
fclose(stdout);
}
freopen("fast_write_2.txt", "a", stdout);
printf("\n");
printf("All: fast_write_2 used time: %.5f s.\n", all_time);
printf("Average: fast_write_2 used time: %.5f s.", all_time/5);
return 0;
}
Linux下的战绩:
#1: fast_write_2 used time: 0.98863 s.
#2: fast_write_2 used time: 0.99541 s.
#3: fast_write_2 used time: 0.96855 s.
#4: fast_write_2 used time: 0.97093 s.
#5: fast_write_2 used time: 0.96418 s.
All: fast_write_2 used time: 4.88771 s.
Average: fast_write_2 used time: 0.97754 s.
Windows下的战绩:
#1: fast_write_2 used time: 1.59600 s.
#2: fast_write_2 used time: 1.57100 s.
#3: fast_write_2 used time: 1.58100 s.
#4: fast_write_2 used time: 1.59000 s.
#5: fast_write_2 used time: 1.58400 s.
All: fast_write_2 used time: 7.92200 s.
Average: fast_write_2 used time: 1.58440 s.
还是比递归的慢了一点……不过已经可以啦!
毕竟它的时间复杂度(算常数)为 。比递归版的 还是慢一点的。
时间就是金钱,效率就是生命!
(8)快写+fwrite(递归)
众所周知,还有一种更加快速的方式:
char buffer[1 << 21];
int pp = -1;
const int pp2 = (1 << 21) - 1;
inline void flush() {
fwrite(buffer, 1, pp + 1, stdout),
pp = -1;
}
inline void putc(const char &x) {
if (pp == pp2) flush();
buffer[++pp] = x;
}
inline void write(int x){
if (x < 0){
putc('-');
x = -x;
}
int tmp = x/10;
if (x > 9) write(tmp);
putc(x-(tmp<<1)-(tmp<<3)+'0');
}
没错,就是 fwrite。
fwrite 的用法和 fread 差不多,在此不再赘述。
测试代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t strt_time, end_time;
double run_time, all_time;
string num, file;
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline long long read() {
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
return x*f;
}
char buffer[1 << 21];
int pp = -1;
const int pp2 = (1 << 21) - 1;
inline void flush() {
fwrite(buffer, 1, pp + 1, stdout),
pp = -1;
}
inline void putc(const char &x) {
if (pp == pp2) flush();
buffer[++pp] = x;
}
inline void write(int x){
if (x < 0){
putc('-');
x = -x;
}
int tmp = x/10;
if (x > 9) write(tmp);
putc(x-(tmp<<1)-(tmp<<3)+'0');
}
int main(){
freopen("fwrite_1.txt", "w", stdout);
fclose(stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
freopen("fwrite_1 out.txt", "w", stdout);
n = read();
for (int j=1; j<=n; j++) a[i][j] = read();
fclose(stdin);
strt_time = clock();
write(n);
putc('\n');
for (int j=1; j<=n; j++){
write(a[i][j]);
putc('\n');
}
end_time = clock();
fclose(stdout);
freopen("fwrite_1.txt", "a", stdout);
run_time = (double)(end_time-strt_time)/CLOCKS_PER_SEC;
printf("#%d: fwrite_1 used time: %.5f s.\n", i, run_time);
all_time += run_time;
fclose(stdout);
}
freopen("fwrite_1.txt", "a", stdout);
printf("\n");
printf("All: fwrite_1 used time: %.5f s.\n", all_time);
printf("Average: fwrite_1 used time: %.5f s.", all_time/5);
return 0;
}
在Linux下的战绩:
#1: fwrite_1 used time: 0.24768 s.
#2: fwrite_1 used time: 0.24807 s.
#3: fwrite_1 used time: 0.25220 s.
#4: fwrite_1 used time: 0.25583 s.
#5: fwrite_1 used time: 0.25383 s.
All: fwrite_1 used time: 1.25762 s.
Average: fwrite_1 used time: 0.25152 s.
在Windows下的战绩:
#1: fwrite_1 used time: 0.45900 s.
#2: fwrite_1 used time: 0.45300 s.
#3: fwrite_1 used time: 0.45100 s.
#4: fwrite_1 used time: 0.44400 s.
#5: fwrite_1 used time: 0.44300 s.
All: fwrite_1 used time: 2.25000 s.
Average: fwrite_1 used time: 0.45000 s.
比前面的输出方式都快了许多呢!
(9)快写+fwrite(非递归)
就是上面的快读改成了非递归的,就变成了下面的模样:
char buffer[1 << 21];
int pp = -1;
const int pp2 = (1 << 21) - 1;
inline void flush() {
fwrite(buffer, 1, pp + 1, stdout),
pp = -1;
}
inline void putc(const char &x) {
if (pp == pp2) flush();
buffer[++pp] = x;
}
inline void write(int x){
if (x < 0){
putc('-');
x = -x;
}
int st[20], p = 0, tmp;
while (x){
tmp = x/10;
st[++p] = x-(tmp<<1)-(tmp<<3);
x = tmp;
}
while (p > 0) putc(st[p--]+'0');
}
测试代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+1;
long long n;
long long a[6][N];
clock_t strt_time, end_time;
double run_time, all_time;
string num, file;
char buf[1 << 20], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) \
? EOF \
: *p1++)
inline long long read() {
long long x = 0, f = 1;
char ch;
while (!isdigit(ch = gc())){
if (ch == '-') f = -1;
}
do{
x = (x << 1) + (x << 3) + (ch ^ 48);
}while (isdigit(ch = gc()));
return x*f;
}
char buffer[1 << 21];
int pp = -1;
const int pp2 = (1 << 21) - 1;
inline void flush() {
fwrite(buffer, 1, pp + 1, stdout),
pp = -1;
}
inline void putc(const char &x) {
if (pp == pp2) flush();
buffer[++pp] = x;
}
inline void write(int x){
if (x < 0){
putc('-');
x = -x;
}
int st[20], p = 0, tmp;
while (x){
tmp = x/10;
st[++p] = x-(tmp<<1)-(tmp<<3);
x = tmp;
}
while (p > 0) putc(st[p--]+'0');
}
int main(){
freopen("fwrite_2.txt", "w", stdout);
fclose(stdout);
for (int i=1; i<=5; i++){
num = to_string(i);
file = "test #" + num + ".txt";
freopen(file.c_str(), "r", stdin);
freopen("fwrite_2 out.txt", "w", stdout);
n = read();
for (int j=1; j<=n; j++) a[i][j] = read();
fclose(stdin);
strt_time = clock();
write(n);
putc('\n');
for (int j=1; j<=n; j++){
write(a[i][j]);
putc('\n');
}
end_time = clock();
fclose(stdout);
freopen("fwrite_2.txt", "a", stdout);
run_time = (double)(end_time-strt_time)/CLOCKS_PER_SEC;
printf("#%d: fwrite_2 used time: %.5f s.\n", i, run_time);
all_time += run_time;
fclose(stdout);
}
freopen("fwrite_2.txt", "a", stdout);
printf("\n");
printf("All: fwrite_2 used time: %.5f s.\n", all_time);
printf("Average: fwrite_2 used time: %.5f s.", all_time/5);
return 0;
}
Linux下战绩:
#1: fwrite_2 used time: 0.25688 s.
#2: fwrite_2 used time: 0.25829 s.
#3: fwrite_2 used time: 0.27216 s.
#4: fwrite_2 used time: 0.26379 s.
#5: fwrite_2 used time: 0.26136 s.
All: fwrite_2 used time: 1.31249 s.
Average: fwrite_2 used time: 0.26250 s.
Windows下战绩:
#1: fwrite_2 used time: 0.46000 s.
#2: fwrite_2 used time: 0.47500 s.
#3: fwrite_2 used time: 0.47200 s.
#4: fwrite_2 used time: 0.45400 s.
#5: fwrite_2 used time: 0.45400 s.
All: fwrite_2 used time: 2.31500 s.
Average: fwrite_2 used time: 0.46300 s
这个速度还是略逊色于递归版的呀。
(10)输出速度比较(数据数量级为 的平均用时,保留 为小数)
| 项目 | Linux(s) | Windows用时(s) |
|---|---|---|
| cout | ||
| printf | ||
| cout+解除绑定 | ||
| cout+解除绑定+关闭同步 | ||
| 快写(递归) | ||
| 快写(非递归) | ||
| 快写+fwrite(递归) | ||
| 快写+fwrite(非递归) |
3.注意事项
- 在使用本文中的
fwrite+快读 的时候,务必在程序的末尾再调用一次flush()函数。(比如只使用一个文件流的时候) - 在使用本文的
fread()的时候,请提前测试一下你的电脑支不支持,测试代码:
#include <bits/stdc++.h>
using namespace std;
char buf[10];
int main() {
fread(buf,1,sizeof(buf),stdin);
cout << ferror(stdin) << endl;
return 0;
}
编译(请使用C++11)并运行后,输入 123456 + 回车 + Ctrl+Z + 回车。如果输出不是 0。那就说明您的电脑可能不太适合使用 fread(其实就是后果自负的意思)。
- 快读和快写在数据量较小(比如 )的时候,可能会出现负优化,慎用。
参考资料:
读入、输出优化 - OI Wiki (oi-wiki.org)
浅谈C++ IO优化——读优输优方法集锦 - encore 的博客 - 洛谷博客 (luogu.com.cn)
C++ 各类输入输出方式效率比较+输入输出优化[含fread、fwrite版]_EndDavid的博客-CSDN博客_c fread 效率