-
Notifications
You must be signed in to change notification settings - Fork 4
/
bench.cc
173 lines (146 loc) · 4.61 KB
/
bench.cc
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
#include "parsing.hh"
#include <sstream>
#include <benchmark/benchmark.h>
std::string_view example_stringview{example_timestamp, 16};
static void BM_mov(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(1585201087123789);
}
}
static void BM_naive(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(parse_naive(example_stringview));
}
}
static void BM_atoll(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(std::atoll(example_timestamp));
}
}
static void BM_sstream(benchmark::State& state) {
std::stringstream s(example_timestamp);
for (auto _ : state) {
s.seekg(0);
std::uint64_t i = 0;
s >> i;
benchmark::DoNotOptimize(i);
// TODO: reset
}
}
static void BM_charconv(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(parse_char_conv(example_stringview));
}
}
#ifdef QND_TEST_BOOST
static void BM_boost_spirit(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(parse_qi(example_stringview));
}
}
#endif
static void BM_unrolled(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(parse_unrolled(example_stringview));
}
}
static void BM_trick(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(parse_trick(example_stringview));
}
}
static void BM_trick_simd(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(parse_trick_simd(example_stringview));
}
}
BENCHMARK(BM_mov);
BENCHMARK(BM_atoll);
BENCHMARK(BM_sstream);
BENCHMARK(BM_charconv);
#ifdef QND_TEST_BOOST
BENCHMARK(BM_boost_spirit);
#endif
BENCHMARK(BM_naive);
BENCHMARK(BM_unrolled);
BENCHMARK(BM_trick);
BENCHMARK(BM_trick_simd);
static void BM_charconv_range(benchmark::State& state) {
char buffer[32] = {0,};
std::sprintf(buffer, "%ld", state.range(0));
const char* last = buffer + sizeof(buffer);
for (auto _ : state) {
std::uint64_t result = 0;
std::from_chars(buffer, last, result);
benchmark::DoNotOptimize(result);
}
}
static void BM_general_trick_simd_range(benchmark::State& state) {
char buffer[32] = {0,};
std::sprintf(buffer, "%ld", state.range(0));
const char* last = buffer + sizeof(buffer);
for (auto _ : state) {
std::uint64_t result = 0;
from_chars(buffer, last, result);
benchmark::DoNotOptimize(result);
}
}
static void BM_charconv_short_string_range(benchmark::State& state) {
auto buffer = std::to_string(state.range(0));
const char* first = buffer.data();
const char* last = first + buffer.size();
for (auto _ : state) {
std::uint64_t result = 0;
std::from_chars(first, last, result);
benchmark::DoNotOptimize(result);
}
}
static void BM_general_trick_simd_short_string_range(benchmark::State& state) {
auto buffer = std::to_string(state.range(0));
const char* first = buffer.data();
const char* last = first + buffer.size();
for (auto _ : state) {
std::uint64_t result = 0;
from_chars(first, last, result);
benchmark::DoNotOptimize(result);
}
}
// from_chars benchmarks
BENCHMARK(BM_charconv_range)->Range(1ull, 9999999999999999ull);
BENCHMARK(BM_general_trick_simd_range)->Range(1ull, 9999999999999999ull);
BENCHMARK(BM_charconv_short_string_range)->Range(1ull, 999999999999999ull);
BENCHMARK(BM_general_trick_simd_short_string_range)->Range(1ull, 999999999999999ull);
static void BM_tzcnt(benchmark::State& state) {
const volatile std::uint64_t value = 0x00000fffffffffff;
for (auto _ : state) {
benchmark::DoNotOptimize(__tzcnt_u64(~value));
}
}
static void BM_get_digit_count_from_numeric_mask(benchmark::State& state) {
const volatile __m128i value = _mm_set1_epi8(0xff);
for (auto _ : state) {
auto result = detail::get_digit_count_from_numeric_mask(value);
benchmark::DoNotOptimize(result);
}
}
static void BM_shift_bytes_left(benchmark::State& state) {
std::uint64_t num_bytes = state.range(0);
const volatile __m128i value = _mm_set1_epi8(0xff);
for (auto _ : state) {
__m128i result = detail::shift_bytes_left(value, num_bytes);
benchmark::DoNotOptimize(result);
}
}
static void BM_shift_bytes_left_branchless(benchmark::State& state) {
std::uint64_t num_bytes = state.range(0);
const volatile __m128i value = _mm_set1_epi8(0xff);
for (auto _ : state) {
__m128i result = detail::shift_bytes_left_branchless(value, num_bytes);
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_tzcnt);
BENCHMARK(BM_get_digit_count_from_numeric_mask);
BENCHMARK(BM_shift_bytes_left)->DenseRange(0, 16, 1);
BENCHMARK(BM_shift_bytes_left_branchless)->DenseRange(0, 16, 1);
BENCHMARK_MAIN();