今天没事闲着做了个测试。。。读入 1e7
个 998244353
以下的整数。。。
编译器 | scanf | fscanf | cin 关同步 | ifstream |
---|---|---|---|---|
MinGW -O2 -std=c++11 (O2 的影响比较小 ) | 14.28 | 12.846 | 5.181 | 4.667 |
WSL -O2 -std=c++11 (O2 的影响比较小 ) | 3.265 | 4.234 | 1.453 | 1.281 |
Visual Studio Release | 5.976 | 5.669 | 11.841 | 12.673 |
- 在比赛环境下,
cin
和ifstream
一般比scanf
快 - 关同步的
cin
和ifstream
差不多 - Visual Studio是毒瘤
- WSL居然比MinGW快,Linux真的NB(
代码就放这儿了,欢迎重现测试结果,欢迎 noilinux 用户进行测试,有 Bug 我不管啊(
// -std=c++11 -O2
#include "bits/stdc++.h"
using namespace std;
void gen()
{
ofstream of("a.txt");
int n = 1e7; of << n << '\n';
std::random_device rd; std::mt19937 mt(rd());
for (int i = 1; i <= n; ++i) {
of << mt() % 998244353 << ' ';
}
of << '\n';
of.close();
}
void checkfs1()
{
int n;
freopen("a.txt", "r", stdin);
scanf("%d", &n);
int sig = 0;
for (int i = 1; i <= n; ++i) {
int x; scanf("%d", &x);
sig ^= x;
}
printf("%d\n", sig);
fclose(stdin);
}
void checkfs2()
{
int n;
FILE *f = fopen("a.txt", "r");
fscanf(f, "%d", &n);
int sig = 0;
for (int i = 1; i <= n; ++i) {
int x; fscanf(f, "%d", &x);
sig ^= x;
}
printf("%d\n", sig);
fclose(f);
}
void checkfs3()
{
int n; freopen("a.txt", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
cin >> n;
int sig = 0;
for (int i = 1; i <= n; ++i) {
int x; cin >> x; sig ^= x;
}
printf("%d\n", sig);
fclose(stdin);
}
void checkfs4()
{
int n; ifstream f("a.txt");
f >> n;
int sig = 0;
for (int i = 1; i <= n; ++i) {
int x; f >> x; sig ^= x;
}
printf("%d\n", sig);
f.close();
}
template<typename T> double timer(T f)
{
auto x = clock();
f();
auto y = clock() - x;
double z = y; z /= CLOCKS_PER_SEC;
return z;
}
int main()
{
double tg = timer(gen);
double t1 = timer(checkfs1);
double t2 = timer(checkfs2);
double t3 = timer(checkfs3);
double t4 = timer(checkfs4);
cout << tg << ' ' << t1 << ' ' << t2 << ' ' << t3 << ' ' << t4 << endl;
return 0;
}