sorted_vector_types does not work with std::inserter
[folly.git] / folly / test / SortedVectorBenchmark.cpp
1 /*
2  * Copyright 2014 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
17 #include "folly/Format.h"
18
19 #include <glog/logging.h>
20
21 #include "folly/sorted_vector_types.h"
22 #include "folly/Benchmark.h"
23
24 namespace {
25
26 using folly::sorted_vector_map;
27
28 sorted_vector_map<int, int> a;
29 sorted_vector_map<int, int> b;
30
31 BENCHMARK(merge_by_setting, iters) {
32   while (iters--) {
33     // copy to match merge benchmark
34     auto a_cpy = a;
35     auto b_cpy = b;
36     for (const auto& kv : b_cpy) {
37       a_cpy[kv.first] = kv.second;
38     }
39   }
40 }
41
42 BENCHMARK_RELATIVE(merge, iters) {
43   while (iters--) {
44     auto a_cpy = a;
45     auto b_cpy = b;
46     merge(a_cpy, b_cpy);
47   }
48 }
49 }
50
51 // Benchmark results on my dev server (Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz)
52 //
53 // ============================================================================
54 // folly/test/SortedVectorBenchmark.cpp            relative  time/iter  iters/s
55 // ============================================================================
56 // merge_by_setting                                           482.01us    2.07K
57 // merge                                           2809.19%    17.16us   58.28K
58 // ============================================================================
59
60 int main(int argc, char *argv[]) {
61   google::ParseCommandLineFlags(&argc, &argv, true);
62   for (int i = 0; i < 1000; ++i) {
63     a[2 * i] = 2 * i;
64     b[2 * i + 1] = 2 * i + 1;
65   }
66
67   folly::runBenchmarks();
68   return 0;
69 }