-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.cpp
235 lines (196 loc) · 5.99 KB
/
benchmark.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "benchmark/benchmark.h"
#include "inja/inja.hpp"
#include "webxx.h"
#include <array>
using namespace Webxx;
typedef const char* Input;
constexpr static const char* helloWorld{"Hello world."};
constexpr static const char* something{"something"};
constexpr static const char* somethingElse{"Something else."};
constexpr size_t nMany{1000};
constexpr std::array<size_t,nMany> nItems{};
////| |////
////| Single element |////
////| |////
std::string renderSingleElementInja (Input input) {
inja::json data;
data["input"] = input;
return inja::render("<h1>{{ input }}</h1>", data);
}
static void singleElementInja (benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(renderSingleElementInja(helloWorld));
benchmark::ClobberMemory();
}
}
BENCHMARK(singleElementInja);
std::string renderSingleElementWebxx (Input input) {
return render(h1{input});
}
static void singleElementWebxx (benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(renderSingleElementWebxx(helloWorld));
benchmark::ClobberMemory();
}
}
BENCHMARK(singleElementWebxx);
std::string renderSingleElementSprintf (Input input) {
char buffer[64];
snprintf(buffer, 64, "<h1>%s</h1>", input);
return buffer;
}
static void singleElementSprintf (benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(renderSingleElementSprintf(helloWorld));
benchmark::ClobberMemory();
}
}
BENCHMARK(singleElementSprintf);
std::string renderSingleElementStringAppend (Input input) {
std::string html;
html.append("<h1>");
html.append(input);
html.append("</h1>");
return html;
}
static void singleElementStringAppend (benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(renderSingleElementStringAppend(helloWorld));
benchmark::ClobberMemory();
}
}
BENCHMARK(singleElementStringAppend);
////| |////
////| Multi-element |////
////| |////
std::string renderMultiElementInja (Input a, Input b, Input c) {
inja::json data;
data["a"] = a;
data["b"] = b;
data["c"] = c;
return inja::render("<div class=\"{{ b }}\"><h1>{{ a }}</h1><p>{{ c }}</p></div>", data);
}
static void multiElementInja (benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(renderMultiElementInja(helloWorld, something, somethingElse));
benchmark::ClobberMemory();
}
}
BENCHMARK(multiElementInja);
std::string renderMultiElementWebxx (Input a, Input b, Input c) {
return render(dv{{_class{b}},
h1{a},
p{c},
});
}
static void multiElementWebxx (benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(renderMultiElementWebxx(helloWorld, something, somethingElse));
benchmark::ClobberMemory();
}
}
BENCHMARK(multiElementWebxx);
std::string renderMultiElementSprintf (Input a, Input b, Input c) {
char buffer[128];
snprintf(buffer, 128, "<div class=\"%s\"><h1>%s</h1><p>%s</p></div>", b, a, c);
return buffer;
}
static void multiElementSprintf (benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(renderMultiElementSprintf(helloWorld, something, somethingElse));
benchmark::ClobberMemory();
}
}
BENCHMARK(multiElementSprintf);
std::string renderMultiStringAppend (Input a, Input b, Input c) {
std::string html;
html.append("<div class=\"");
html.append(b);
html.append("\">");
html.append("<h1>");
html.append(a);
html.append("</h1>");
html.append("<p>");
html.append(c);
html.append("</p>");
html.append("</div>");
return html;
}
static void multiElementStringAppend (benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(renderMultiStringAppend(helloWorld, something, somethingElse));
benchmark::ClobberMemory();
}
}
BENCHMARK(multiElementStringAppend);
////| |////
////| Many-element |////
////| |////
std::string render1kElementInja (Input a, Input b, Input c) {
inja::json data;
data["n"] = nItems;
data["a"] = a;
data["b"] = b;
data["c"] = c;
return inja::render(
"<ol>"
"{% for i in n %}"
"<li class=\"{{ b }}\">{{ loop.index }}<h1>{{ a }}</h1><p>{{ c }}</p></li>"
"{% endfor %}"
"</ol>"
, data);
}
static void loop1kInja (benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(render1kElementInja(helloWorld, something, somethingElse));
benchmark::ClobberMemory();
}
}
BENCHMARK(loop1kInja);
std::string render1kElementWebxx (Input a, Input b, Input c) {
return render(ol{
loop(nItems, [a,b,c] (const auto&, const Loop& loop) {
return li{{_class{b}},
std::to_string(loop.index),
h1{a},
p{c},
};
}),
});
}
static void loop1kWebxx (benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(render1kElementWebxx(helloWorld, something, somethingElse));
benchmark::ClobberMemory();
}
}
BENCHMARK(loop1kWebxx);
std::string render1kStringAppend (Input a, Input b, Input c) {
std::string html;
html.append("<ol>");
int i{0};
for (auto& _ : nItems) {
html.append("<li class=\"");
html.append(b);
html.append("\">");
html.append(std::to_string(i)),
html.append("<h1>");
html.append(a);
html.append("</h1>");
html.append("<p>");
html.append(c);
html.append("</p>");
html.append("</li>");
++i;
}
html.append("</ol>");
return html;
}
static void loop1kStringAppend (benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(render1kStringAppend(helloWorld, something, somethingElse));
benchmark::ClobberMemory();
}
}
BENCHMARK(loop1kStringAppend);
BENCHMARK_MAIN();