Debian Codegraph @

benchmarks/bench_str_map.cpp

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