Fix copyright lines for Bits.h and move BitsBenchmark.cpp
[folly.git] / folly / lang / test / BitsBenchmark.cpp
1 /*
2  * Copyright 2016-present 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/lang/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 BENCHMARK_DRAW_LINE();
42 BENCHMARK(reverse, iters) {
43   uint64_t b = 0;
44   for (unsigned long i = 0; i < iters; ++i) {
45     b = folly::bitReverse(i + b);
46     folly::doNotOptimizeAway(b);
47   }
48 }
49
50 int main(int argc, char** argv) {
51   gflags::ParseCommandLineFlags(&argc, &argv, true);
52   folly::runBenchmarks();
53   return 0;
54 }
55
56 /*
57 Benchmarks run on dual Xeon X5650's @ 2.67GHz w/hyperthreading enabled
58   (12 physical cores, 12 MB cache, 72 GB RAM)
59
60 ============================================================================
61 folly/test/BitsBenchmark.cpp                    relative  time/iter  iters/s
62 ============================================================================
63 nextPowTwoClz                                                0.00fs  Infinity
64 ----------------------------------------------------------------------------
65 isPowTwo                                                   731.61ps    1.37G
66 ----------------------------------------------------------------------------
67 reverse                                                      4.84ns  206.58M
68 ============================================================================
69 */