-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
62 lines (59 loc) · 2.01 KB
/
benchmark.cpp
File metadata and controls
62 lines (59 loc) · 2.01 KB
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
#include <chrono>
#include <iostream>
#include <smallstring/smallstring.hpp>
double __attribute__((noinline)) smallstring_benchmark() {
smallstring::Buffer buff{2048};
std::chrono::high_resolution_clock::time_point start, end;
{
start = std::chrono::high_resolution_clock::now();
for (int j = 0; j < 100; j++) {
buff.clear();
for (int i = 0; i < 100; i++) {
buff.push("hello");
}
}
end = std::chrono::high_resolution_clock::now();
}
auto time =
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start)
.count();
// hopefully this stops the compiler from optimizing out the loop lol
static volatile int tmp = buff.view().size();
static volatile std::string_view tmp1 = buff.view();
return time;
}
double __attribute__((noinline)) stdstring_benchmark() {
std::string str;
str.reserve(2048);
std::chrono::high_resolution_clock::time_point start, end;
{
start = std::chrono::high_resolution_clock::now();
for (int j = 0; j < 100; j++) {
str.clear();
for (int i = 0; i < 100; i++) {
str.append("hello");
}
}
end = std::chrono::high_resolution_clock::now();
}
auto time =
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start)
.count();
static volatile int tmp = str.size();
static volatile std::string_view tmp1 = str;
return time;
}
int main() {
int runs = 10000;
double smallstr_average_time = 0;
double stdstr_average_time = 0;
for (int i = 0; i < runs; i++) {
smallstr_average_time += smallstring_benchmark() / ((double)runs);
stdstr_average_time += stdstring_benchmark() / ((double)runs);
}
std::cout << "Results:\n"
<< " - smallstr::Buffer = " << smallstr_average_time << "ns\n"
<< " - std::string = " << stdstr_average_time << "ns"
<< std::endl;
return 0;
}