| 1 |
#include <benchmark/benchmark.h> |
| 2 |
|
| 3 |
#include <frozen/set.h> |
| 4 |
#include <frozen/string.h> |
| 5 |
|
| 6 |
#include <set> |
| 7 |
#include <array> |
| 8 |
#include <string> |
| 9 |
#include <algorithm> |
| 10 |
|
| 11 |
static constexpr frozen::set<frozen::string, 32> Keywords{ |
| 12 |
"auto", "break", "case", "char", "const", "continue", |
| 13 |
"default", "do", "double", "else", "enum", "extern", |
| 14 |
"float", "for", "goto", "if", "int", "long", |
| 15 |
"register", "return", "short", "signed", "sizeof", "static", |
| 16 |
"struct", "switch", "typedef", "union", "unsigned", "void", |
| 17 |
"volatile", "while"}; |
| 18 |
|
| 19 |
static auto const* volatile Some = &Keywords; |
| 20 |
|
| 21 |
static void BM_StrInFzSet(benchmark::State& state) { |
| 22 |
for (auto _ : state) { |
| 23 |
for(auto kw : *Some) { |
| 24 |
volatile bool status = Keywords.count(kw); |
| 25 |
benchmark::DoNotOptimize(status); |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
BENCHMARK(BM_StrInFzSet); |
| 30 |
|
| 31 |
static const std::set<frozen::string> Keywords_(Keywords.begin(), Keywords.end()); |
| 32 |
|
| 33 |
static void BM_StrInStdSet(benchmark::State& state) { |
| 34 |
for (auto _ : state) { |
| 35 |
for(auto kw : *Some) { |
| 36 |
volatile bool status = Keywords_.count(kw); |
| 37 |
benchmark::DoNotOptimize(status); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
BENCHMARK(BM_StrInStdSet); |
| 43 |
|
| 44 |
static const std::array<frozen::string, 32> Keywords__{ |
| 45 |
"auto", "break", "case", "char", "const", "continue", |
| 46 |
"default", "do", "double", "else", "enum", "extern", |
| 47 |
"float", "for", "goto", "if", "int", "long", |
| 48 |
"register", "return", "short", "signed", "sizeof", "static", |
| 49 |
"struct", "switch", "typedef", "union", "unsigned", "void", |
| 50 |
"volatile", "while"}; |
| 51 |
|
| 52 |
static void BM_StrInStdArray(benchmark::State& state) { |
| 53 |
for (auto _ : state) { |
| 54 |
for(auto kw : *Some) { |
| 55 |
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end(); |
| 56 |
benchmark::DoNotOptimize(status); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
BENCHMARK(BM_StrInStdArray); |
| 62 |
|
| 63 |
|
| 64 |
static const frozen::string SomeStrings[32] = { |
| 65 |
"auto0", "break0", "case0", "char0", "const0", "continue0", |
| 66 |
"default0", "do0", "double0", "else0", "enum0", "extern0", |
| 67 |
"float0", "for0", "goto0", "if0", "int0", "long0", |
| 68 |
"register0", "return0", "short0", "signed0", "sizeof0", "static0", |
| 69 |
"struct0", "switch0", "typedef0", "union0", "unsigned0", "void0", |
| 70 |
"volatile0", "while0"}; |
| 71 |
static auto const * volatile SomeStringsPtr = &SomeStrings; |
| 72 |
|
| 73 |
static void BM_StrNotInFzSet(benchmark::State& state) { |
| 74 |
for (auto _ : state) { |
| 75 |
for(auto kw : *SomeStringsPtr) { |
| 76 |
volatile bool status = Keywords.count(kw); |
| 77 |
benchmark::DoNotOptimize(status); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
BENCHMARK(BM_StrNotInFzSet); |
| 82 |
|
| 83 |
static void BM_StrNotInStdSet(benchmark::State& state) { |
| 84 |
for (auto _ : state) { |
| 85 |
for(auto kw : *SomeStringsPtr) { |
| 86 |
volatile bool status = Keywords_.count(kw); |
| 87 |
benchmark::DoNotOptimize(status); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
BENCHMARK(BM_StrNotInStdSet); |
| 92 |
|
| 93 |
static void BM_StrNotInStdArray(benchmark::State& state) { |
| 94 |
for (auto _ : state) { |
| 95 |
for(auto kw : *SomeStringsPtr) { |
| 96 |
volatile bool status = std::find(Keywords__.begin(), Keywords__.end(), kw) != Keywords__.end(); |
| 97 |
benchmark::DoNotOptimize(status); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
BENCHMARK(BM_StrNotInStdArray); |