#include<cstdio> usingnamespace std; intmain() { freopen("input.in", "w", stdout); for (int i = 1; i <= 20000000; i++) printf("%d ", i); printf("\n"); return0; }
1. 流输入输出 关闭同步流
Test1.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<iostream> #include<cstdio> usingnamespace std; intmain() { int t; ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); for (int i = 1; i <= 20000000; i++) cin >> t; for (int i = 1; i <= 20000000; i++) cout << i << " "; cout << "\n"; return0; }
编译命令:
1
g++ Test1.cpp -o2 -o Test1.exe
gcc 13.2.1
三次结果:
平均结果:6525 ms
gcc 9.5.0:
三次结果:
平均结果:6451 ms
2. C标准输入输出
Test2.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream> #include<cstdio> usingnamespace std; intmain() { int t; for (int i = 1; i <= 20000000; i++) scanf("%d", &t); for (int i = 1; i <= 20000000; i++) printf("%d ", i); printf("\n"); return0; }
编译命令:
1
g++ Test2.cpp -o2 -o Test2.exe
gcc 13.2.1:
三次结果:
平均结果:7310 ms
gcc 9.5.0:
三次结果:
平均结果:7483 ms
3. 流输入输出 不关闭同步流
Test3.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<iostream> #include<cstdio> usingnamespace std; intmain() { int t; // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); for (int i = 1; i <= 20000000; i++) cin >> t; for (int i = 1; i <= 20000000; i++) cout << i << " "; cout << "\n"; return0; }