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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
#include <stdio.h>
#include <emscripten.h>
#include <bind.h>
#include <memory>
int counter = 0;
extern "C"
{
int __attribute__((noinline)) get_counter()
{
return counter;
}
void __attribute__((noinline)) increment_counter()
{
++counter;
}
int __attribute__((noinline)) sum_int(int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8, int v9)
{
return v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
}
float __attribute__((noinline)) sum_float(float v1, float v2, float v3, float v4, float v5, float v6, float v7, float v8, float v9)
{
return v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
}
int __attribute__((noinline)) returns_input(int i)
{
return i;
}
extern void increment_counter_benchmark_js(int N);
extern void returns_input_benchmark_js();
extern void sum_int_benchmark_js();
extern void sum_float_benchmark_js();
extern void increment_counter_benchmark_embind_js(int N);
extern void returns_input_benchmark_embind_js();
extern void sum_int_benchmark_embind_js();
extern void sum_float_benchmark_embind_js();
extern void increment_class_counter_benchmark_embind_js(int N);
extern void move_gameobjects_benchmark_embind_js();
}
class Vec3
{
public:
Vec3():x(0),y(0),z(0) {}
Vec3(float x_, float y_, float z_):x(x_),y(y_),z(z_) {}
float x,y,z;
};
Vec3 add(const Vec3 &lhs, const Vec3 &rhs) { return Vec3(lhs.x+rhs.x, lhs.y+rhs.y, lhs.z+rhs.z); }
class Transform
{
public:
Transform():scale(1) {}
Vec3 pos;
Vec3 rot;
float scale;
Vec3 GetPosition() { return pos; }
Vec3 GetRotation() { return rot; }
float GetScale() { return scale; }
void SetPosition(const Vec3 &pos_) { pos = pos_; }
void SetRotation(const Vec3 &rot_) { rot = rot_; }
void SetScale(float scale_) { scale = scale_; }
};
typedef std::shared_ptr<Transform> TransformPtr;
class GameObject
{
public:
GameObject()
{
transform = std::make_shared<Transform>();
}
std::shared_ptr<Transform> transform;
TransformPtr GetTransform() { return transform; }
};
typedef std::shared_ptr<GameObject> GameObjectPtr;
GameObjectPtr create_game_object()
{
return std::make_shared<GameObject>();
}
class Foo
{
public:
Foo()
:class_counter(0)
{
}
void __attribute__((noinline)) incr_global_counter()
{
++counter;
}
void __attribute__((noinline)) incr_class_counter()
{
++class_counter;
}
int class_counter_val() const
{
return class_counter;
}
int class_counter;
};
EMSCRIPTEN_BINDINGS(benchmark)
{
using namespace emscripten;
class_<GameObject>("GameObject")
.smart_ptr<GameObjectPtr>()
.function("GetTransform", &GameObject::GetTransform);
class_<Transform>("Transform")
.smart_ptr<TransformPtr>()
.function("GetPosition", &Transform::GetPosition)
.function("GetRotation", &Transform::GetRotation)
.function("GetScale", &Transform::GetScale)
.function("SetPosition", &Transform::SetPosition)
.function("SetRotation", &Transform::SetRotation)
.function("SetScale", &Transform::SetScale);
value_tuple<Vec3>("Vec3")
.element(&Vec3::x)
.element(&Vec3::y)
.element(&Vec3::z);
function("create_game_object", &create_game_object);
function("add", &add);
function("get_counter", &get_counter);
function("increment_counter", &increment_counter);
function("returns_input", &returns_input);
function("sum_int", &sum_int);
function("sum_float", &sum_float);
class_<Foo>("Foo")
.constructor<>()
.function("incr_global_counter", &Foo::incr_global_counter)
.function("incr_class_counter", &Foo::incr_class_counter)
.function("class_counter_val", &Foo::class_counter_val);
}
void __attribute__((noinline)) emscripten_get_now_benchmark(int N)
{
volatile float t = emscripten_get_now();
for(int i = 0; i < N; ++i)
{
emscripten_get_now();
emscripten_get_now();
emscripten_get_now();
emscripten_get_now();
emscripten_get_now();
emscripten_get_now();
emscripten_get_now();
emscripten_get_now();
emscripten_get_now();
emscripten_get_now();
}
volatile float t2 = emscripten_get_now();
printf("C++ emscripten_get_now %d iters: %f msecs.\n", N, 1000.f*(t2-t));
}
void __attribute__((noinline)) increment_counter_benchmark(int N)
{
volatile float t = emscripten_get_now();
for(int i = 0; i < N; ++i)
{
increment_counter();
increment_counter();
increment_counter();
increment_counter();
increment_counter();
increment_counter();
increment_counter();
increment_counter();
increment_counter();
increment_counter();
}
volatile float t2 = emscripten_get_now();
printf("C++ increment_counter %d iters: %f msecs.\n", N, 1000.f*(t2-t));
}
void __attribute__((noinline)) increment_class_counter_benchmark(int N)
{
Foo foo;
volatile float t = emscripten_get_now();
for(int i = 0; i < N; ++i)
{
foo.incr_class_counter();
foo.incr_class_counter();
foo.incr_class_counter();
foo.incr_class_counter();
foo.incr_class_counter();
foo.incr_class_counter();
foo.incr_class_counter();
foo.incr_class_counter();
foo.incr_class_counter();
foo.incr_class_counter();
}
volatile float t2 = emscripten_get_now();
printf("C++ increment_class_counter %d iters: %f msecs. result: %d\n", N, 1000.f*(t2-t), foo.class_counter);
}
void __attribute__((noinline)) returns_input_benchmark()
{
volatile int r = 0;
volatile float t = emscripten_get_now();
for(int i = 0; i < 100000; ++i)
{
r += returns_input(i);
r += returns_input(i);
r += returns_input(i);
r += returns_input(i);
r += returns_input(i);
r += returns_input(i);
r += returns_input(i);
r += returns_input(i);
r += returns_input(i);
r += returns_input(i);
}
volatile float t2 = emscripten_get_now();
printf("C++ returns_input 100000 iters: %f msecs.\n", 1000.f*(t2-t));
}
void __attribute__((noinline)) sum_int_benchmark()
{
volatile float t = emscripten_get_now();
volatile int r = 0;
for(int i = 0; i < 100000; ++i)
{
r += sum_int(i,2,3,4,5,6,7,8,9);
r += sum_int(i,2,3,4,5,6,7,8,9);
r += sum_int(i,2,3,4,5,6,7,8,9);
r += sum_int(i,2,3,4,5,6,7,8,9);
r += sum_int(i,2,3,4,5,6,7,8,9);
r += sum_int(i,2,3,4,5,6,7,8,9);
r += sum_int(i,2,3,4,5,6,7,8,9);
r += sum_int(i,2,3,4,5,6,7,8,9);
r += sum_int(i,2,3,4,5,6,7,8,9);
r += sum_int(i,2,3,4,5,6,7,8,9);
}
volatile float t2 = emscripten_get_now();
printf("C++ sum_int 100000 iters: %f msecs.\n", 1000.f*(t2-t));
}
void __attribute__((noinline)) sum_float_benchmark()
{
volatile float f = 0.f;
volatile float t = emscripten_get_now();
for(int i = 0; i < 100000; ++i)
{
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
f += sum_float((float)i,2.f,3.f,4.f,5.f,6.f,7.f,8.f,9.f);
}
volatile float t2 = emscripten_get_now();
printf("C++ sum_float 100000 iters: %f msecs.\n", 1000.f*(t2-t));
}
void __attribute__((noinline)) move_gameobjects_benchmark()
{
const int N = 100000;
GameObjectPtr objects[N];
for(int i = 0; i < N; ++i)
objects[i] = create_game_object();
volatile float t = emscripten_get_now();
for(int i = 0; i < N; ++i)
{
TransformPtr t = objects[i]->GetTransform();
Vec3 pos = add(t->GetPosition(), Vec3(2.f, 0.f, 1.f));
Vec3 rot = add(t->GetRotation(), Vec3(0.1f, 0.2f, 0.3f));
t->SetPosition(pos);
t->SetRotation(rot);
}
volatile float t2 = emscripten_get_now();
Vec3 accum;
for(int i = 0; i < N; ++i)
accum = add(add(accum, objects[i]->GetTransform()->GetPosition()), objects[i]->GetTransform()->GetRotation());
printf("C++ move_gameobjects %d iters: %f msecs. Result: %f\n", N, 1000.f*(t2-t), accum.x+accum.y+accum.z);
}
int main()
{
for(int i = 1000; i <= 100000; i *= 10)
emscripten_get_now_benchmark(i);
printf("\n");
for(int i = 1000; i <= 100000; i *= 10)
{
increment_counter_benchmark(i);
increment_counter_benchmark_js(i);
increment_counter_benchmark_embind_js(i);
printf("\n");
}
for(int i = 1000; i <= 100000; i *= 10)
{
increment_class_counter_benchmark(i);
increment_class_counter_benchmark_embind_js(i);
printf("\n");
}
returns_input_benchmark();
returns_input_benchmark_js();
returns_input_benchmark_embind_js();
printf("\n");
sum_int_benchmark();
sum_int_benchmark_js();
sum_int_benchmark_embind_js();
printf("\n");
sum_float_benchmark();
sum_float_benchmark_js();
sum_float_benchmark_embind_js();
printf("\n");
move_gameobjects_benchmark();
move_gameobjects_benchmark_embind_js();
}
|