2017
[folly.git] / folly / experimental / test / StringKeyedBenchmark.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 // Copyright 2013-present Facebook. All Rights Reserved.
17
18 #include <folly/Benchmark.h>
19 #include <folly/Range.h>
20
21 #include <map>
22 #include <set>
23 #include <string>
24 #include <unordered_map>
25 #include <unordered_set>
26
27 #include <folly/experimental/StringKeyedMap.h>
28 #include <folly/experimental/StringKeyedSet.h>
29 #include <folly/experimental/StringKeyedUnorderedMap.h>
30 #include <folly/experimental/StringKeyedUnorderedSet.h>
31
32 using folly::StringKeyedMap;
33 using folly::StringKeyedSet;
34 using folly::StringKeyedUnorderedMap;
35 using folly::StringKeyedUnorderedSet;
36 using folly::StringPiece;
37 using std::map;
38 using std::to_string;
39 using std::set;
40 using std::string;
41 using std::unordered_map;
42 using std::unordered_set;
43
44 static map<string, int> m;
45 static StringKeyedMap<int> skm;
46 static set<string> s;
47 static StringKeyedSet sks;
48 static unordered_map<string, int> um;
49 static StringKeyedUnorderedMap<int> skum;
50 static unordered_set<string> us;
51 static StringKeyedUnorderedSet skus;
52 static const string lookup("123");
53 static const folly::StringPiece lookupPiece(lookup);
54
55 static void initBenchmarks() {
56   for (int i = 0; i < 1000; ++i) {
57     auto iStr = to_string(i);
58     m[iStr] = i;
59     skm.insert(make_pair(iStr, i));
60     um[iStr] = i;
61     skum.insert(make_pair(iStr, i));
62     s.insert(iStr);
63     sks.insert(iStr);
64     us.insert(iStr);
65     skus.insert(iStr);
66   }
67 }
68
69 BENCHMARK(std_map_benchmark_find) {
70   folly::doNotOptimizeAway(m.find(lookupPiece.str())->second);
71 }
72
73 BENCHMARK_RELATIVE(sk_map_benchmark_find) {
74   folly::doNotOptimizeAway(skm.find(lookupPiece)->second);
75 }
76
77 BENCHMARK(std_map_benchmark_erase_emplace) {
78   m.erase(lookup);
79   m.emplace(lookup, 123);
80 }
81
82 BENCHMARK_RELATIVE(sk_map_benchmark_erase_emplace) {
83   skm.erase(lookup);
84   skm.emplace(lookup, 123);
85 }
86
87 BENCHMARK(std_unordered_map_benchmark_find) {
88   folly::doNotOptimizeAway(um.find(lookupPiece.str())->second);
89 }
90
91 BENCHMARK_RELATIVE(sk_unordered_map_benchmark_find) {
92   folly::doNotOptimizeAway(skum.find(lookupPiece)->second);
93 }
94
95 BENCHMARK(std_unordered_map_benchmark_erase_emplace) {
96   um.erase(lookup);
97   um.emplace(lookup, 123);
98 }
99
100 BENCHMARK_RELATIVE(sk_unordered_map_benchmark_erase_emplace) {
101   skum.erase(lookup);
102   skum.emplace(lookup, 123);
103 }
104
105 BENCHMARK(std_set_benchmark_find) {
106   folly::doNotOptimizeAway(s.find(lookupPiece.str()));
107 }
108
109 BENCHMARK_RELATIVE(sk_set_benchmark_find) {
110   folly::doNotOptimizeAway(sks.find(lookupPiece));
111 }
112
113 BENCHMARK(std_set_benchmark_erase_emplace) {
114   s.erase(lookup);
115   s.emplace(lookup);
116 }
117
118 BENCHMARK_RELATIVE(sk_set_benchmark_erase_emplace) {
119   sks.erase(lookup);
120   sks.emplace(lookup);
121 }
122
123 BENCHMARK(std_unordered_set_benchmark_find) {
124   folly::doNotOptimizeAway(us.find(lookupPiece.str()));
125 }
126
127 BENCHMARK_RELATIVE(sk_unordered_set_benchmark_find) {
128   folly::doNotOptimizeAway(skus.find(lookupPiece));
129 }
130
131 BENCHMARK(std_unordered_set_benchmark_erase_emplace) {
132   us.erase(lookup);
133   us.emplace(lookup);
134 }
135
136 BENCHMARK_RELATIVE(sk_unordered_set_benchmark_erase_emplace) {
137   skus.erase(lookup);
138   skus.emplace(lookup);
139 }
140
141 int main(int argc, char **argv) {
142   gflags::ParseCommandLineFlags(&argc, &argv, true);
143   initBenchmarks();
144   folly::runBenchmarks();
145 }