2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 // Copyright 2013-present Facebook. All Rights Reserved.
18 #include <folly/Benchmark.h>
19 #include <folly/Range.h>
24 #include <unordered_map>
25 #include <unordered_set>
27 #include <folly/experimental/StringKeyedMap.h>
28 #include <folly/experimental/StringKeyedSet.h>
29 #include <folly/experimental/StringKeyedUnorderedMap.h>
30 #include <folly/experimental/StringKeyedUnorderedSet.h>
32 using folly::StringKeyedMap;
33 using folly::StringKeyedSet;
34 using folly::StringKeyedUnorderedMap;
35 using folly::StringKeyedUnorderedSet;
36 using folly::StringPiece;
41 using std::unordered_map;
42 using std::unordered_set;
44 static map<string, int> m;
45 static StringKeyedMap<int> skm;
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);
55 static void initBenchmarks() {
56 for (int i = 0; i < 1000; ++i) {
57 auto iStr = to_string(i);
59 skm.insert(make_pair(iStr, i));
61 skum.insert(make_pair(iStr, i));
69 BENCHMARK(std_map_benchmark_find) {
70 folly::doNotOptimizeAway(m.find(lookupPiece.str())->second);
73 BENCHMARK_RELATIVE(sk_map_benchmark_find) {
74 folly::doNotOptimizeAway(skm.find(lookupPiece)->second);
77 BENCHMARK(std_map_benchmark_erase_emplace) {
79 m.emplace(lookup, 123);
82 BENCHMARK_RELATIVE(sk_map_benchmark_erase_emplace) {
84 skm.emplace(lookup, 123);
87 BENCHMARK(std_unordered_map_benchmark_find) {
88 folly::doNotOptimizeAway(um.find(lookupPiece.str())->second);
91 BENCHMARK_RELATIVE(sk_unordered_map_benchmark_find) {
92 folly::doNotOptimizeAway(skum.find(lookupPiece)->second);
95 BENCHMARK(std_unordered_map_benchmark_erase_emplace) {
97 um.emplace(lookup, 123);
100 BENCHMARK_RELATIVE(sk_unordered_map_benchmark_erase_emplace) {
102 skum.emplace(lookup, 123);
105 BENCHMARK(std_set_benchmark_find) {
106 folly::doNotOptimizeAway(s.find(lookupPiece.str()));
109 BENCHMARK_RELATIVE(sk_set_benchmark_find) {
110 folly::doNotOptimizeAway(sks.find(lookupPiece));
113 BENCHMARK(std_set_benchmark_erase_emplace) {
118 BENCHMARK_RELATIVE(sk_set_benchmark_erase_emplace) {
123 BENCHMARK(std_unordered_set_benchmark_find) {
124 folly::doNotOptimizeAway(us.find(lookupPiece.str()));
127 BENCHMARK_RELATIVE(sk_unordered_set_benchmark_find) {
128 folly::doNotOptimizeAway(skus.find(lookupPiece));
131 BENCHMARK(std_unordered_set_benchmark_erase_emplace) {
136 BENCHMARK_RELATIVE(sk_unordered_set_benchmark_erase_emplace) {
138 skus.emplace(lookup);
141 int main(int argc, char **argv) {
143 folly::runBenchmarks();