2017
[folly.git] / folly / test / BitsBenchmark.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
17 // @author Tudor Bosman (tudorb@fb.com)
18
19 #include <folly/Bits.h>
20
21 #include <folly/Benchmark.h>
22
23 using namespace folly;
24
25 BENCHMARK(nextPowTwoClz, iters) {
26   for (unsigned long i = 0; i < iters; ++i) {
27     auto x = folly::nextPowTwo(iters);
28     folly::doNotOptimizeAway(x);
29   }
30 }
31
32 BENCHMARK_DRAW_LINE();
33 BENCHMARK(isPowTwo, iters) {
34   bool b;
35   for (unsigned long i = 0; i < iters; ++i) {
36     b = folly::isPowTwo(i);
37     folly::doNotOptimizeAway(b);
38   }
39 }
40
41 int main(int argc, char** argv) {
42   gflags::ParseCommandLineFlags(&argc, &argv, true);
43   folly::runBenchmarks();
44   return 0;
45 }
46
47 /*
48 Benchmarks run on dual Xeon X5650's @ 2.67GHz w/hyperthreading enabled
49   (12 physical cores, 12 MB cache, 72 GB RAM)
50
51 Benchmark                               Iters   Total t    t/iter iter/sec
52 ------------------------------------------------------------------------------
53 *       nextPowTwoClz                 1000000  1.659 ms  1.659 ns  574.8 M
54 */