c++读入比较

数据: 1e7

数据范围: int

重定向文件读入

优化: 无

cin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> k;
}
}
/*
real 0m8.912s
user 0m8.844s
sys 0m0.048s
*/

cin 取消兼容stdio

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n, k;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> k;
}
}
/*
real 0m1.897s
user 0m1.844s
sys 0m0.032s
*/

scanf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &k);
}
}
/*
real 0m2.180s
user 0m2.140s
sys 0m0.028s
*/

getchar()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# include <bits/stdc++.h>
using namespace std;
inline int gn() {
int k = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) k = k * 10 + c - '0';
return k * f;
}
int main() {
int n, k;
n = gn();
for(int i = 1; i <= n; i++) {
k = gn();
}
}
/*
real 0m1.642s
user 0m1.588s
sys 0m0.052s
*/

fread()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# include <bits/stdc++.h>
using namespace std;
inline char getc() {
static char buf[1 << 18], *fs, *ft;
return (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 18, stdin)), fs == ft) ? EOF : *fs++;
}
inline int gn() {
register int k = 0, f = 1;
register char c = getc();
for(; !isdigit(c); c = getc()) if(c == '-') f = -1;
for(; isdigit(c); c = getc()) k = k * 10 + c - '0';
return k * f;
}
int main() {
int n, k;
n = gn();
for(int i = 1; i <= n; i++) {
k = gn();
}
}
/*
real 0m0.648s
user 0m0.608s
sys 0m0.036s
*/

显而易见,在OI系列比赛中,快读保平安233