(不考虑读优的)读入速度测试

今天没事闲着做了个测试。。。读入 1e7998244353 以下的整数。。。

编译器 scanffscanfcin 关同步 ifstream
MinGW -O2 -std=c++11 (O2 的影响比较小 )14.2812.8465.1814.667
WSL -O2 -std=c++11 (O2 的影响比较小 )3.2654.2341.4531.281
Visual Studio Release5.9765.66911.84112.673
结论:
  • 在比赛环境下,cinifstream 一般比 scanf
  • 关同步的 cinifstream 差不多
  • 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;
}