-
Notifications
You must be signed in to change notification settings - Fork 3
/
std_vs_pyincpp.cpp
353 lines (312 loc) · 11.8 KB
/
std_vs_pyincpp.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
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
345
346
347
348
349
350
351
352
353
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>
#include <deque>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include "../sources/pyincpp.hpp"
TEST_CASE("std::vector vs pyincpp::List", "[std]")
{
auto s = std::vector<int>{1, 2, 3, 4, 5};
auto p = pyincpp::List<int>{1, 2, 3, 4, 5};
REQUIRE(std::vector<int>{1, 2, 3, 4, 5} == s);
BENCHMARK("ctor (std)")
{
return std::vector<int>{1, 2, 3, 4, 5};
};
REQUIRE(pyincpp::List<int>{1, 2, 3, 4, 5} == p);
BENCHMARK("ctor (pyincpp)")
{
return pyincpp::List<int>{1, 2, 3, 4, 5};
};
s.push_back(6);
REQUIRE(s == std::vector<int>{1, 2, 3, 4, 5, 6});
BENCHMARK("append (std)")
{
s.push_back(6);
};
p += 6;
REQUIRE(p == pyincpp::List<int>{1, 2, 3, 4, 5, 6});
BENCHMARK("append (pyincpp)")
{
p += 6;
};
REQUIRE(s.at(0) == 1);
BENCHMARK("access (std)")
{
return s.at(0);
};
REQUIRE(p[0] == 1);
BENCHMARK("access (pyincpp)")
{
return p[0]; // with boundary check
};
}
TEST_CASE("std::set vs pyincpp::Set", "[std]")
{
auto s = std::set<int>{1, 2, 3, 4, 5};
auto p = pyincpp::Set<int>{1, 2, 3, 4, 5};
REQUIRE(std::set<int>{1, 2, 3, 4, 5} == s);
BENCHMARK("ctor (std)")
{
return std::set<int>{1, 2, 3, 4, 5};
};
REQUIRE(pyincpp::Set<int>{1, 2, 3, 4, 5} == p);
BENCHMARK("ctor (pyincpp)")
{
return pyincpp::Set<int>{1, 2, 3, 4, 5};
};
s.insert(6);
REQUIRE(s == std::set<int>{1, 2, 3, 4, 5, 6});
BENCHMARK("add (std)")
{
return s.insert(6);
};
p.add(6);
REQUIRE(p == pyincpp::Set<int>{1, 2, 3, 4, 5, 6});
BENCHMARK("add (pyincpp)")
{
return p.add(6);
};
REQUIRE(*s.find(2) == 2);
BENCHMARK("find (std)")
{
return *s.find(2);
};
REQUIRE(*p.find(2) == 2);
BENCHMARK("find (pyincpp)")
{
return *p.find(2);
};
}
TEST_CASE("std::map vs pyincpp::Dict", "[std]")
{
auto s = std::map<int, char>{{1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'}, {5, 'E'}};
auto p = pyincpp::Dict<int, char>{{1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'}, {5, 'E'}};
REQUIRE(std::map<int, char>{{1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'}, {5, 'E'}} == s);
BENCHMARK("ctor (std)")
{
return std::map<int, char>{{1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'}, {5, 'E'}};
};
REQUIRE(pyincpp::Dict<int, char>{{1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'}, {5, 'E'}} == p);
BENCHMARK("ctor (pyincpp)")
{
return pyincpp::Dict<int, char>{{1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'}, {5, 'E'}};
};
s.insert({6, 'F'});
REQUIRE(s == std::map<int, char>{{1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'}, {5, 'E'}, {6, 'F'}});
BENCHMARK("add (std)")
{
return s.insert({6, 'F'});
};
p.add(6, 'F');
REQUIRE(p == pyincpp::Dict<int, char>{{1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'}, {5, 'E'}, {6, 'F'}});
BENCHMARK("add (pyincpp)")
{
return p.add(6, 'F');
};
REQUIRE(s[5] == 'E');
BENCHMARK("access (std)")
{
return s[5];
};
REQUIRE(p[5] == 'E');
BENCHMARK("access (pyincpp)")
{
return p[5];
};
}
TEST_CASE("std::string vs pyincpp::Str", "[std]")
{
auto s = std::string("hello world");
auto p = pyincpp::Str("hello world");
REQUIRE(std::string("hello world") == s);
BENCHMARK("ctor (std)")
{
return std::string("hello world");
};
REQUIRE(pyincpp::Str("hello world") == p);
BENCHMARK("ctor (pyincpp)")
{
return pyincpp::Str("hello world");
};
REQUIRE(s + "!" == "hello world!");
BENCHMARK("append (std)")
{
return s + "!";
};
REQUIRE(p + "!" == "hello world!");
BENCHMARK("append (pyincpp)")
{
return p + "!";
};
REQUIRE(s.find("world") == 6);
BENCHMARK("find (std)")
{
return s.find("world");
};
REQUIRE(p.find("world") == 6);
BENCHMARK("find (pyincpp)")
{
return p.find("world");
};
}
TEST_CASE("std::tuple vs pyincpp::Tuple", "[std]")
{
auto s = std::make_tuple(1, 2.33, 'A');
auto p = pyincpp::make_tuple(1, 2.33, 'A');
REQUIRE(std::make_tuple(1, 2.33, 'A') == s);
BENCHMARK("make_tuple (std)")
{
return std::make_tuple(1, 2.33, 'A');
};
REQUIRE(pyincpp::make_tuple(1, 2.33, 'A') == p);
BENCHMARK("make_tuple (pyincpp)")
{
return pyincpp::make_tuple(1, 2.33, 'A');
};
REQUIRE(s == s);
BENCHMARK("cmp (std)")
{
return s == s;
};
REQUIRE(p == p);
BENCHMARK("cmp (pyincpp)")
{
return p == p;
};
REQUIRE(std::get<0>(s) == 1);
BENCHMARK("get (std)")
{
return std::get<0>(s);
};
REQUIRE(p.get<0>() == 1);
BENCHMARK("get (pyincpp)")
{
return p.get<0>();
};
}
TEST_CASE("std::deque vs pyincpp::Deque", "[std]")
{
auto s = std::deque<int>{1, 2, 3, 4, 5};
auto p = pyincpp::Deque<int>{1, 2, 3, 4, 5};
REQUIRE(std::deque<int>{1, 2, 3, 4, 5} == s);
BENCHMARK("ctor (std)")
{
return std::deque<int>{1, 2, 3, 4, 5};
};
REQUIRE(pyincpp::Deque<int>{1, 2, 3, 4, 5} == p);
BENCHMARK("ctor (pyincpp)")
{
return pyincpp::Deque<int>{1, 2, 3, 4, 5};
};
s.push_back(6);
REQUIRE(s.back() == 6);
BENCHMARK("push_back (std)")
{
return s.push_back(6);
};
p.push_back(6);
REQUIRE(p.back() == 6);
BENCHMARK("push_back (pyincpp)")
{
return p.push_back(6);
};
s.push_front(0);
REQUIRE(s.front() == 0);
BENCHMARK("push_front (std)")
{
return s.push_front(0);
};
p.push_front(0);
REQUIRE(p.front() == 0);
BENCHMARK("push_front (pyincpp)")
{
return p.push_front(0);
};
}
/*
Run with: `xmake config -m release && xmake build bench && xmake run bench --benchmark-no-analysis -i [std]`
Result (2024.12.24, Windows 10, Catch2 v3.7.1):
-------------------------------------------------------------------------------
std::vector vs pyincpp::List
-------------------------------------------------------------------------------
benches\std_vs_pyincpp.cpp(13)
...............................................................................
benchmark name samples iterations mean
-------------------------------------------------------------------------------
ctor (std) 100 203 67.0542 ns
ctor (pyincpp) 100 271 68.4022 ns
append (std) 100 2596 2.68952 ns
append (pyincpp) 100 2407 3.086 ns
access (std) 100 19754 0.914397 ns
access (pyincpp) 100 16606 1.11484 ns
-------------------------------------------------------------------------------
std::set vs pyincpp::Set
-------------------------------------------------------------------------------
benches\std_vs_pyincpp.cpp(54)
...............................................................................
benchmark name samples iterations mean
-------------------------------------------------------------------------------
ctor (std) 100 44 431.159 ns
ctor (pyincpp) 100 44 430.295 ns
add (std) 100 2829 7.26935 ns
add (pyincpp) 100 2077 7.49206 ns
find (std) 100 5949 3.11548 ns
find (pyincpp) 100 5854 3.14315 ns
-------------------------------------------------------------------------------
std::map vs pyincpp::Dict
-------------------------------------------------------------------------------
benches\std_vs_pyincpp.cpp(95)
...............................................................................
benchmark name samples iterations mean
-------------------------------------------------------------------------------
ctor (std) 100 43 431.86 ns
ctor (pyincpp) 100 44 434.977 ns
add (std) 100 2832 6.57804 ns
add (pyincpp) 100 2067 8.04838 ns
access (std) 100 3165 5.69826 ns
access (pyincpp) 100 3657 4.76347 ns
-------------------------------------------------------------------------------
std::string vs pyincpp::Str
-------------------------------------------------------------------------------
benches\std_vs_pyincpp.cpp(136)
...............................................................................
benchmark name samples iterations mean
-------------------------------------------------------------------------------
ctor (std) 100 7765 2.37411 ns
ctor (pyincpp) 100 7779 2.3773 ns
append (std) 100 2399 7.93497 ns
append (pyincpp) 100 1209 15.7279 ns
find (std) 100 2709 6.83204 ns
find (pyincpp) 100 1286 15.0101 ns
-------------------------------------------------------------------------------
std::tuple vs pyincpp::Tuple
-------------------------------------------------------------------------------
benches\std_vs_pyincpp.cpp(175)
...............................................................................
benchmark name samples iterations mean
-------------------------------------------------------------------------------
make_tuple (std) 100 30953 0.597293 ns
make_tuple (pyincpp) 100 31151 0.593496 ns
cmp (std) 100 18348 0.979398 ns
cmp (pyincpp) 100 17862 1.07384 ns
get (std) 100 30168 0.613697 ns
get (pyincpp) 100 30145 0.620335 ns
-------------------------------------------------------------------------------
std::deque vs pyincpp::Deque
-------------------------------------------------------------------------------
benches\std_vs_pyincpp.cpp(214)
...............................................................................
benchmark name samples iterations mean
-------------------------------------------------------------------------------
ctor (std) 100 68 270.691 ns
ctor (pyincpp) 100 70 268.557 ns
push_back (std) 100 984 17.0264 ns
push_back (pyincpp) 100 984 16.6839 ns
push_front (std) 100 889 17.0484 ns
push_front (pyincpp) 100 849 18.3133 ns
===============================================================================
*/