proxygen
BitIteratorBench.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-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 
18 
19 #include <algorithm>
20 
21 #include <glog/logging.h>
22 
23 #include <folly/Benchmark.h>
24 #include <folly/init/Init.h>
25 #include <folly/small_vector.h>
26 
27 using namespace folly;
28 
29 namespace {
30 
31 template <class BaseIter>
32 BitIterator<BaseIter> simpleFFS(
35  return std::find(begin, end, true);
36 }
37 
38 template <class FFS>
39 void runFFSTest(FFS fn) {
40  constexpr size_t const maxblocks = 3;
41  constexpr size_t const bpb = 8 * sizeof(uint64_t);
42  for (size_t nblocks = 1; nblocks <= maxblocks; ++nblocks) {
44  size_t nbits = nblocks * bpb;
45  auto begin = makeBitIterator(data.cbegin());
46  auto end = makeBitIterator(data.cend());
47  DCHECK_EQ(nbits, end - begin);
48  DCHECK(begin != end);
49 
50  // Try every possible combination of first bit set (including none),
51  // start bit, end bit
52  for (size_t firstSet = 0; firstSet <= nbits; ++firstSet) {
53  data.assign(nblocks, 0);
54  if (firstSet) {
55  size_t b = firstSet - 1;
56  data[b / bpb] |= (1ULL << (b % bpb));
57  }
58  for (size_t startBit = 0; startBit <= nbits; ++startBit) {
59  for (size_t endBit = startBit; endBit <= nbits; ++endBit) {
60  auto p = begin + startBit;
61  auto q = begin + endBit;
62  p = fn(p, q);
64  if (firstSet < startBit + 1 || firstSet >= endBit + 1) {
65  DCHECK_EQ(endBit, p - begin)
66  << " firstSet=" << firstSet << " startBit=" << startBit
67  << " endBit=" << endBit << " nblocks=" << nblocks;
68  } else {
69  DCHECK_EQ(firstSet - 1, p - begin)
70  << " firstSet=" << firstSet << " startBit=" << startBit
71  << " endBit=" << endBit << " nblocks=" << nblocks;
72  }
73  }
74  }
75  }
76  }
77 }
78 
79 void runSimpleFFSTest(int iters) {
80  while (iters--) {
81  runFFSTest([](auto first, auto last) { return simpleFFS(first, last); });
82  }
83 }
84 
85 void runRealFFSTest(int iters) {
86  while (iters--) {
87  runFFSTest([](auto first, auto last) { return findFirstSet(first, last); });
88  }
89 }
90 
91 } // namespace
92 
93 BENCHMARK(SimpleFFSTest, iters) {
94  runSimpleFFSTest(iters);
95 }
96 BENCHMARK(RealFFSTest, iters) {
97  runRealFFSTest(iters);
98 }
99 
100 /* --bm_min_iters=10 --bm_max_iters=100
101 
102 Benchmark Iters Total t t/iter iter/sec
103 ------------------------------------------------------------------------------
104 runSimpleFFSTest 10 4.82 s 482 ms 2.075
105 runRealFFSTest 19 2.011 s 105.9 ms 9.447
106 
107 */
108 
109 int main(int argc, char** argv) {
110  folly::init(&argc, &argv);
112  return 0;
113 }
char b
BitIterator< BaseIter > findFirstSet(BitIterator< BaseIter >, BitIterator< BaseIter >)
Definition: BitIterator.h:170
BitIterator< BaseIter > makeBitIterator(const BaseIter &iter)
Definition: BitIterator.h:161
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void runBenchmarks()
Definition: Benchmark.cpp:456
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
char ** argv
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
int main(int argc, char **argv)
BENCHMARK(fbFollyGlobalBenchmarkBaseline)
Definition: Benchmark.cpp:84
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
constexpr detail::First first
Definition: Base-inl.h:2553