| 1 |
#include <benchmark/benchmark.h> |
| 2 |
|
| 3 |
#include <frozen/set.h> |
| 4 |
|
| 5 |
#include <set> |
| 6 |
#include <array> |
| 7 |
#include <algorithm> |
| 8 |
|
| 9 |
static constexpr frozen::set<int, 32> Keywords{ |
| 10 |
0, 2, 4, 6, 8, 10, 12, 14, |
| 11 |
16, 18, 20, 22, 24, 26, 28, 30, |
| 12 |
32, 34, 36, 38, 40, 42, 44, 46, |
| 13 |
48, 50, 52, 54, 56, 58, 60, 62 |
| 14 |
}; |
| 15 |
|
| 16 |
static auto const* volatile Some = &Keywords; |
| 17 |
|
| 18 |
static void BM_IntInFzSet(benchmark::State& state) { |
| 19 |
for (auto _ : state) { |
| 20 |
for(auto kw : *Some) { |
| 21 |
volatile bool status = Keywords.count(kw); |
| 22 |
benchmark::DoNotOptimize(status); |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
BENCHMARK(BM_IntInFzSet); |
| 27 |
|
| 28 |
static const std::set<int> Keywords_(Keywords.begin(), Keywords.end()); |
| 29 |
|
| 30 |
static void BM_IntInStdSet(benchmark::State& state) { |
| 31 |
for (auto _ : state) { |
| 32 |
for(auto kw : *Some) { |
| 33 |
volatile bool status = Keywords_.count(kw); |
| 34 |
benchmark::DoNotOptimize(status); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
BENCHMARK(BM_IntInStdSet); |
| 40 |
|
| 41 |
static const std::array<int, 32> Keywords__{{ |
| 42 |
0, 2, 4, 6, 8, 10, 12, 14, |
| 43 |
16, 18, 20, 22, 24, 26, 28, 30, |
| 44 |
32, 34, 36, 38, 40, 42, 44, 46, |
| 45 |
48, 50, 52, 54, 56, 58, 60, 62 |
| 46 |
}}; |
| 47 |
static void BM_IntInStdArray(benchmark::State& state) { |
| 48 |
for (auto _ : state) { |
| 49 |
for(auto kw : *Some) { |
| 50 |
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end(); |
| 51 |
benchmark::DoNotOptimize(status); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
BENCHMARK(BM_IntInStdArray); |
| 57 |
|
| 58 |
static const int SomeInts[32] = { |
| 59 |
1, 3, 5, 7, 9, 11, 13, 15, |
| 60 |
17, 19, 21, 23, 25, 27, 29, 31, |
| 61 |
33, 35, 37, 39, 41, 43, 45, 47, |
| 62 |
49, 51, 53, 55, 57, 59, 61, 63 |
| 63 |
}; |
| 64 |
static auto const * volatile SomeIntsPtr = &SomeInts; |
| 65 |
|
| 66 |
static void BM_IntNotInFzSet(benchmark::State& state) { |
| 67 |
for (auto _ : state) { |
| 68 |
for(auto kw : *SomeIntsPtr) { |
| 69 |
volatile bool status = Keywords.count(kw); |
| 70 |
benchmark::DoNotOptimize(status); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
BENCHMARK(BM_IntNotInFzSet); |
| 75 |
|
| 76 |
static void BM_IntNotInStdSet(benchmark::State& state) { |
| 77 |
for (auto _ : state) { |
| 78 |
for(auto kw : *SomeIntsPtr) { |
| 79 |
volatile bool status = Keywords_.count(kw); |
| 80 |
benchmark::DoNotOptimize(status); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
BENCHMARK(BM_IntNotInStdSet); |
| 85 |
|
| 86 |
static void BM_IntNotInStdArray(benchmark::State& state) { |
| 87 |
for (auto _ : state) { |
| 88 |
for(auto kw : *SomeIntsPtr) { |
| 89 |
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end(); |
| 90 |
benchmark::DoNotOptimize(status); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
BENCHMARK(BM_IntNotInStdArray); |