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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <cctype>
using namespace std;
const int N=10000000,NC=10*N;
int a[N],swtime,swsize;
char s[NC];
char sw[16*N];
void gen() {
for (int i=0;i<N;i++) {
a[i]=rand();
}
for (int i=0;i<NC;i++) {
s[i]=rand()%10+'0';
}
int cur=clock();
swsize=0;
for (int i=0;i<N;i++) {
char s[100];
sprintf(s,"%d",a[i]);
for (int j=0;s[j];j++) {
sw[swsize++]=s[j];
}
sw[swsize++]=' ';
}
swtime=clock()-cur;
}
void test_printf_int() {
int cur=clock();
FILE *f = fopen("temp.txt", "w");
for (int i=0;i<N;i++)
fprintf(f, "%d ",a[i]);
fclose(f);
fprintf(stderr,"Test: (printf, %d ints) %.2lfc\n",N,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_cout_int() {
int cur=clock();
ofstream f("temp.txt");
for (int i=0;i<N;i++)
f << a[i] << ' ';
f.close();
fprintf(stderr,"Test: (cout, %d ints) %.2lfc\n",N,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_write_int() {
int cur=clock();
ofstream f("temp.txt");
f.write(sw,swsize);
f.close();
fprintf(stderr,"Test: (write, %d ints, %d chars) %.2lfc + %.2lfc\n",N,swsize,(swtime+.0)/CLOCKS_PER_SEC,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_fwrite_int() {
int cur=clock();
FILE *f = fopen("temp.txt", "w");
fwrite(sw,1,swsize,f);
fclose(f);
fprintf(stderr,"Test: (fwrite, %d ints, %d chars) %.2lfc + %.2lfc\n",N,swsize,(swtime+.0)/CLOCKS_PER_SEC,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_scanf_int() {
int cur=clock();
FILE *f = fopen("temp.txt", "r");
for (int i=0;i<N;i++) {
int t;
fscanf(f, "%d",&t);
if (t!=a[i]) {
fprintf(stderr,"Fail with scanf\n");
exit(1);
}
}
fclose(f);
fprintf(stderr,"Test: (scanf, %d ints) %.2lfc\n",N,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_cin_int() {
int cur=clock();
ifstream f("temp.txt");
for (int i=0;i<N;i++) {
int t;
f >> t;
if (t!=a[i]) {
fprintf(stderr,"Fail with cin\n");
exit(1);
}
}
f.close();
fprintf(stderr,"Test: (cin, %d ints) %.2lfc\n",N,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_printf_char() {
int cur=clock();
FILE *f = fopen("temp.txt", "w");
for (int i=0;i<NC;i++)
fprintf(f, "%c",s[i]);
fclose(f);
fprintf(stderr,"Test: (printf, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_cout_char() {
int cur=clock();
ofstream f("temp.txt");
for (int i=0;i<NC;i++)
f << s[i];
f.close();
fprintf(stderr,"Test: (cout, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_putchar_char() {
int cur=clock();
FILE *f = fopen("temp.txt", "w");
for (int i=0;i<NC;i++)
fputc(s[i], f);
fclose(f);
fprintf(stderr,"Test: (putchar, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_scanf_char() {
int cur=clock();
FILE *f = fopen("temp.txt", "r");
for (int i=0;i<NC;i++) {
char t;
fscanf(f, "%c",&t);
if (t!=s[i]) {
fprintf(stderr,"Fail with scanf\n");
exit(1);
}
}
fclose(f);
fprintf(stderr,"Test: (scanf, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_cin_char() {
int cur=clock();
ifstream f("temp.txt");
for (int i=0;i<NC;i++) {
char t;
f >> t;
if (t!=s[i]) {
fprintf(stderr,"Fail with cin\n");
exit(1);
}
}
f.close();
fprintf(stderr,"Test: (cin, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_getchar_char() {
int cur=clock();
FILE *f = fopen("temp.txt", "r");
for (int i=0;i<NC;i++) {
char t = fgetc(f);
if (t!=s[i]) {
fprintf(stderr,"Fail with getchar\n");
exit(1);
}
}
fclose(f);
fprintf(stderr,"Test: (getchar, %d chars) %.2lfc\n",NC,(clock()-cur+.0)/CLOCKS_PER_SEC);
}
void test_read_char() {
int cur=clock();
freopen("temp.txt","r",stdin);
cin.read(sw,NC);
fclose(stdin);
int cur2=clock();
for (int i=0;i<NC;i++)
if (s[i]!=sw[i]) {
cerr << i << ' ' << (int)s[i] << ' ' << (int)sw[i] << endl;
fprintf(stderr,"Fail with read\n");
exit(1);
}
fprintf(stderr,"Test: (read, %d chars) %.2lfc + %.2lfc\n",NC,(cur2-cur+.0)/CLOCKS_PER_SEC,(clock()-cur2+.0)/CLOCKS_PER_SEC);
}
void test_fread_char() {
int cur=clock();
freopen("temp.txt","r",stdin);
fread(sw,1,NC,stdin);
fclose(stdin);
int cur2=clock();
for (int i=0;i<NC;i++)
if (s[i]!=sw[i]) {
fprintf(stderr,"Fail with fread\n");
exit(1);
}
fprintf(stderr,"Test: (fread, %d chars) %.2lfc + %.2lfc\n",NC,(cur2-cur+.0)/CLOCKS_PER_SEC,(clock()-cur2+.0)/CLOCKS_PER_SEC);
}
int main () {
std::ios_base::sync_with_stdio(false);
gen();
test_printf_int();
test_cout_int();
test_write_int();
test_scanf_int();
test_fwrite_int();
test_cin_int();
test_printf_char();
test_scanf_char();
test_cout_char();
test_cin_char();
test_putchar_char();
test_getchar_char();
test_read_char();
test_fread_char();
}
|