-
Notifications
You must be signed in to change notification settings - Fork 16
/
perf.cpp
214 lines (151 loc) · 4.64 KB
/
perf.cpp
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
#include <iostream>
#include <sstream>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <iterator>
#include <stdio.h>
#include <vector>
#include <string.h>
#include "print.h"
int count = 0;
static const int upto = 3000000;
static const char string[] = "This is a string.";
void use_cout_unsync();
void show_time(void (*f)(), char const *caption) {
if (f == use_cout_unsync)
std::cout.sync_with_stdio(false);
clock_t start = clock();
for (count = 0; count < upto; count++)
f();
clock_t ticks = clock()-start;
std::cerr << std::setw(40) << caption
<< ": "
<< (double)ticks/CLOCKS_PER_SEC << "\n";
if (f == use_cout_unsync)
std::cout.sync_with_stdio(true);
}
void use_printf() {
printf("string='%s' count='%d'\n", string, count);
}
// void use_printf_unlocked() {
// for (int i=0; i<count; i++)
// printf_unlocked("string='%s' count='%d'\n", string, count);
// }
void use_puts() {
puts(string);
}
void use_cout() {
std::cout << "string='" << string << "' count='" << count << "'\n";
}
void use_cout_unsync() {
std::cout << "string='" << string << "' count='" << count << "'\n";
}
void use_stringstream() {
std::stringstream temp;
temp << "string='" << string << "' count='" << count << "'\n";
std::cout << temp.str();
}
void use_endl() {
std::cout << "string='" << string << "' count='" << count << "'" << std::endl;
}
void use_fill_n() {
std::fill_n(std::ostream_iterator<char const *>(std::cout, "\n"), count, string);
}
void use_prettyprint() {
print "string='%o' count='%o'" % string, count;
}
void use_putc() {
for (int j = 0; j < (int) sizeof(string); j++) {
//fputc(string[j], stdout);
putc(string[j], stdout);
}
putc('\n', stdout);
}
void use_fputs_unlocked() {
flockfile(stdout);
const char *s1 = "string='";
const char *s2 = "' count='";
const char *s3 = "'\n";
char buf[100];
fputs_unlocked(s1, stdout);
fputs_unlocked(string, stdout);
fputs_unlocked(s2, stdout);
snprintf(buf, sizeof buf, "%d", count);
fputs_unlocked(buf, stdout);
fputs_unlocked(s3, stdout);
funlockfile(stdout);
}
void use_fwrite_unlocked() {
flockfile(stdout);
const char s1[] = "string='";
const char s2[] = "' count='";
const char s3[] = "'\n";
char buf[100];
fwrite_unlocked(s1, sizeof s1, 1, stdout);
fwrite_unlocked(string, strlen(string), 1, stdout);
fwrite_unlocked(s2, sizeof s2, 1, stdout);
int cnt = snprintf(buf, sizeof buf, "%d", count);
fwrite_unlocked(buf, cnt, 1, stdout);
fwrite_unlocked(s3, sizeof s3, 1, stdout);
funlockfile(stdout);
}
void use_memcpy() {
const char s1[] = "string='";
const char s2[] = "' count='";
const char s3[] = "'\n";
char buf[256];
char *ptr = buf;
memcpy(ptr, s1, sizeof(s1)-1);
ptr += sizeof(s1)-1;
int size = strlen(string);
memcpy(ptr, string, size);
ptr += size;
memcpy(ptr, s2, sizeof(s2)-1);
ptr += sizeof(s2)-1;
ptr += snprintf(ptr, 100, "%d", count);
memcpy(ptr, s3, sizeof(s3)-1);
ptr += sizeof(s3)-1;
fwrite(buf, ptr-buf, 1, stdout);
}
void use_fputs() {
const char *s1 = "string='";
const char *s2 = "' count='";
const char *s3 = ";\n";
char buf[100];
fputs(s1, stdout);
fputs(string, stdout);
fputs(s2, stdout);
snprintf(buf, sizeof buf, "%d", count);
fputs(buf, stdout);
fputs(s3, stdout);
}
int main() {
struct PerfCase {
void (*func) ();
const char *desc;
};
std::vector<PerfCase> cases = {
{ &use_printf, "Time using printf" },
{ &use_puts, "Time using puts" },
{ &use_cout, "Time using cout (synced)"},
{ &use_cout_unsync, "Time using cout (un-synced)"},
//{ &use_stringstream, "Time using stringstream"},
{ &use_endl, "Time using endl"},
//{ &use_fill_n, "Time using fill_n"},
{ &use_prettyprint, "Time using pretty print statement"},
{ &use_putc, "Time using putc"},
{ &use_fputs_unlocked, "Time using fputs_unlocked"},
{ &use_fputs, "Time using fputs"},
{ &use_fwrite_unlocked, "Time using fwrite_unlocked"},
{ &use_memcpy, "Time using memcpy"}
};
srand(time(0));
//random_shuffle( cases.begin(), cases.end() );
// show_time(use_memcpy, "x");
// return 0;
for ( auto const& thing : cases ) {
show_time(thing.func, thing.desc);
}
return 0;
}