char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf; #include <bits/stdc++.h> using namespace std; using namespace chrono;
int x; int N = 20000000;
int read() { int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) f ^= (ch == '-'); for(; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch xor 48); return f ? x : -x; }
void gen(int n){ srand(time(0)); FILE *fp = fopen("test.in", "w"); for(int i = 0; i < n; ++i){ int opt = rand() % 2; if(opt) fprintf(fp, "%d ", rand()); else fprintf(fp, "%d ", -rand()); } fclose(fp); }
void test_read(int n){while(n--) x = read();} void test_scanf(int n){while(n--) scanf("%d", &x);} void test_cin(int n){while(n--) cin >> x;}
int main(){ #pragma region 预生成随机数 gen(N); #pragma endregion #pragma region 测试scanf freopen("test.in", "r", stdin); auto start = high_resolution_clock::now(); test_scanf(N); auto end = high_resolution_clock::now(); printf("scanf: %d ms\n", duration_cast<milliseconds>(end - start).count()); fclose(stdin); #pragma endregion #pragma region 测试read #undef getchar freopen("test.in", "r", stdin); start = high_resolution_clock::now(); test_read(N); end = high_resolution_clock::now(); printf("read function: %d ms\n", duration_cast<milliseconds>(end - start).count()); fclose(stdin); #pragma endregion #pragma region 测试fread #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) freopen("test.in", "r", stdin); start = high_resolution_clock::now(); test_read(N); end = high_resolution_clock::now(); printf("fread function: %d ms\n", duration_cast<milliseconds>(end - start).count()); fclose(stdin); #undef getchar #pragma endregion #pragma region 测试cin freopen("test.in", "r", stdin); start = high_resolution_clock::now(); test_cin(N); end = high_resolution_clock::now(); printf("cin: %d ms\n", duration_cast<milliseconds>(end - start).count()); fclose(stdin); #pragma endregion #pragma region 测试untied cin freopen("test.in", "r", stdin); ios::sync_with_stdio(true); cin.tie(nullptr); start = high_resolution_clock::now(); test_cin(N); end = high_resolution_clock::now(); printf("untied cin: %d ms\n", duration_cast<milliseconds>(end - start).count()); fclose(stdin); #pragma endregion #pragma region 测试unsync cin freopen("test.in", "r", stdin); ios::sync_with_stdio(false); start = high_resolution_clock::now(); test_cin(N); end = high_resolution_clock::now(); printf("unsync cin: %d ms\n", duration_cast<milliseconds>(end - start).count()); fclose(stdin); #pragma endregion #pragma region 测试fastest cin freopen("test.in", "r", stdin); ios::sync_with_stdio(false); cin.tie(nullptr); start = high_resolution_clock::now(); test_cin(N); end = high_resolution_clock::now(); printf("fastest cin: %d ms\n", duration_cast<milliseconds>(end - start).count()); fclose(stdin); #pragma endregion return 0; }
|