7
7
#include < cstdint>
8
8
#include < cstdlib>
9
9
#include < ranges>
10
+ #include < string>
11
+ #include < type_traits>
10
12
#include < vector>
11
13
14
+ #include " skewed_allocator.hpp"
15
+
12
16
enum class Op {
13
17
FindSized,
14
18
FindUnsized,
15
19
Count,
20
+ StringFind,
21
+ StringRFind,
16
22
};
17
23
18
24
using namespace std ;
19
25
20
- template <class T , Op Operation>
26
+ template <class T , template < class > class Alloc , Op Operation>
21
27
void bm (benchmark::State& state) {
22
28
const auto size = static_cast <size_t >(state.range (0 ));
23
29
const auto pos = static_cast <size_t >(state.range (1 ));
24
30
25
- vector<T> a (size, T{' 0' });
31
+ using Container = conditional_t <Operation == Op::StringFind || Operation == Op::StringRFind,
32
+ basic_string<T, char_traits<T>, Alloc<T>>, vector<T, Alloc<T>>>;
33
+
34
+ Container a (size, T{' 0' });
26
35
27
36
if (pos < size) {
28
- a[pos] = T{' 1' };
37
+ if constexpr (Operation == Op::StringRFind) {
38
+ a[size - pos - 1 ] = T{' 1' };
39
+ } else {
40
+ a[pos] = T{' 1' };
41
+ }
29
42
} else {
30
43
if constexpr (Operation == Op::FindUnsized) {
31
44
abort ();
@@ -39,6 +52,10 @@ void bm(benchmark::State& state) {
39
52
benchmark::DoNotOptimize (ranges::find (a.begin (), unreachable_sentinel, T{' 1' }));
40
53
} else if constexpr (Operation == Op::Count) {
41
54
benchmark::DoNotOptimize (ranges::count (a.begin (), a.end (), T{' 1' }));
55
+ } else if constexpr (Operation == Op::StringFind) {
56
+ benchmark::DoNotOptimize (a.find (T{' 1' }));
57
+ } else if constexpr (Operation == Op::StringRFind) {
58
+ benchmark::DoNotOptimize (a.rfind (T{' 1' }));
42
59
}
43
60
}
44
61
}
@@ -50,17 +67,28 @@ void common_args(auto bm) {
50
67
}
51
68
52
69
53
- BENCHMARK (bm<uint8_t , Op::FindSized>)->Apply(common_args);
54
- BENCHMARK (bm<uint8_t , Op::FindUnsized>)->Apply(common_args);
55
- BENCHMARK (bm<uint8_t , Op::Count>)->Apply(common_args);
70
+ BENCHMARK (bm<uint8_t , not_highly_aligned_allocator, Op::FindSized>)->Apply(common_args);
71
+ BENCHMARK (bm<uint8_t , highly_aligned_allocator, Op::FindSized>)->Apply(common_args);
72
+ BENCHMARK (bm<uint8_t , not_highly_aligned_allocator, Op::FindUnsized>)->Apply(common_args);
73
+ BENCHMARK (bm<uint8_t , highly_aligned_allocator, Op::FindUnsized>)->Apply(common_args);
74
+ BENCHMARK (bm<uint8_t , not_highly_aligned_allocator, Op::Count>)->Apply(common_args);
75
+ BENCHMARK (bm<uint8_t , highly_aligned_allocator, Op::Count>)->Apply(common_args);
76
+ BENCHMARK (bm<char , not_highly_aligned_allocator, Op::StringFind>)->Apply(common_args);
77
+ BENCHMARK (bm<char , highly_aligned_allocator, Op::StringFind>)->Apply(common_args);
78
+ BENCHMARK (bm<char , not_highly_aligned_allocator, Op::StringRFind>)->Apply(common_args);
79
+ BENCHMARK (bm<char , highly_aligned_allocator, Op::StringRFind>)->Apply(common_args);
56
80
57
- BENCHMARK (bm<uint16_t , Op::FindSized>)->Apply(common_args);
58
- BENCHMARK (bm<uint16_t , Op::Count>)->Apply(common_args);
81
+ BENCHMARK (bm<uint16_t , not_highly_aligned_allocator, Op::FindSized>)->Apply(common_args);
82
+ BENCHMARK (bm<uint16_t , not_highly_aligned_allocator, Op::Count>)->Apply(common_args);
83
+ BENCHMARK (bm<wchar_t , not_highly_aligned_allocator, Op::StringFind>)->Apply(common_args);
84
+ BENCHMARK (bm<wchar_t , not_highly_aligned_allocator, Op::StringRFind>)->Apply(common_args);
59
85
60
- BENCHMARK (bm<uint32_t , Op::FindSized>)->Apply(common_args);
61
- BENCHMARK (bm<uint32_t , Op::Count>)->Apply(common_args);
86
+ BENCHMARK (bm<uint32_t , not_highly_aligned_allocator, Op::FindSized>)->Apply(common_args);
87
+ BENCHMARK (bm<uint32_t , not_highly_aligned_allocator, Op::Count>)->Apply(common_args);
88
+ BENCHMARK (bm<char32_t , not_highly_aligned_allocator, Op::StringFind>)->Apply(common_args);
89
+ BENCHMARK (bm<char32_t , not_highly_aligned_allocator, Op::StringRFind>)->Apply(common_args);
62
90
63
- BENCHMARK (bm<uint64_t , Op::FindSized>)->Apply(common_args);
64
- BENCHMARK (bm<uint64_t , Op::Count>)->Apply(common_args);
91
+ BENCHMARK (bm<uint64_t , not_highly_aligned_allocator, Op::FindSized>)->Apply(common_args);
92
+ BENCHMARK (bm<uint64_t , not_highly_aligned_allocator, Op::Count>)->Apply(common_args);
65
93
66
94
BENCHMARK_MAIN ();
0 commit comments