2 * Copyright 2016 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.
17 #include <folly/Benchmark.h>
18 #include <folly/Foreach.h>
19 #include <folly/String.h>
25 using namespace folly;
33 BENCHMARK(bmFun) { fun(); }
34 BENCHMARK(bmRepeatedFun, n) {
35 FOR_EACH_RANGE (i, 0, n) {
49 BENCHMARK(baselinevector) {
56 FOR_EACH_RANGE (i, 0, 100) {
61 BENCHMARK_RELATIVE(bmVector) {
63 FOR_EACH_RANGE (i, 0, 100) {
64 v.resize(v.size() + 1, 42);
70 BENCHMARK(superslow) {
80 BENCHMARK_MULTI(multiSimple) {
81 FOR_EACH_RANGE (i, 0, 10) {
87 BENCHMARK_RELATIVE_MULTI(multiSimpleRel) {
88 FOR_EACH_RANGE (i, 0, 10) {
95 BENCHMARK_MULTI(multiIterArgs, iter) {
96 FOR_EACH_RANGE (i, 0, 10 * iter) {
102 BENCHMARK_RELATIVE_MULTI(multiIterArgsRel, iter) {
103 FOR_EACH_RANGE (i, 0, 10 * iter) {
110 unsigned paramMulti(unsigned iter, unsigned num) {
111 for (unsigned i = 0; i < iter; ++i) {
112 for (unsigned j = 0; j < num; ++j) {
119 unsigned paramMultiRel(unsigned iter, unsigned num) {
120 for (unsigned i = 0; i < iter; ++i) {
121 for (unsigned j = 0; j < num; ++j) {
129 BENCHMARK_PARAM_MULTI(paramMulti, 1);
130 BENCHMARK_RELATIVE_PARAM_MULTI(paramMultiRel, 1);
132 BENCHMARK_PARAM_MULTI(paramMulti, 5);
133 BENCHMARK_RELATIVE_PARAM_MULTI(paramMultiRel, 5);
135 BENCHMARK_DRAW_LINE();
137 BENCHMARK(BenchmarkSuspender_dismissing_void, iter) {
138 BenchmarkSuspender braces;
141 vector<size_t> v(1 << 12, 0);
142 iota(v.begin(), v.end(), 0);
143 shuffle(v.begin(), v.end(), rng);
144 braces.dismissing([&] {
145 sort(v.begin(), v.end());
150 BENCHMARK(BenchmarkSuspender_dismissing_value, iter) {
151 BenchmarkSuspender braces;
154 vector<size_t> v(1 << 12, 0);
155 iota(v.begin(), v.end(), 0);
156 shuffle(v.begin(), v.end(), rng);
157 auto s = braces.dismissing([&] {
158 sort(v.begin(), v.end());
159 return accumulate(v.begin(), v.end(), 0, [](size_t a, size_t e) {
163 doNotOptimizeAway(s);
167 int main(int argc, char** argv) {
168 gflags::ParseCommandLineFlags(&argc, &argv, true);
170 runBenchmarksOnFlag();