BitIteratorBench.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include <folly/container/BitIterator.h>
  17. #include <algorithm>
  18. #include <glog/logging.h>
  19. #include <folly/Benchmark.h>
  20. #include <folly/init/Init.h>
  21. #include <folly/small_vector.h>
  22. using namespace folly;
  23. namespace {
  24. template <class BaseIter>
  25. BitIterator<BaseIter> simpleFFS(
  26. BitIterator<BaseIter> begin,
  27. BitIterator<BaseIter> end) {
  28. return std::find(begin, end, true);
  29. }
  30. template <class FFS>
  31. void runFFSTest(FFS fn) {
  32. constexpr size_t const maxblocks = 3;
  33. constexpr size_t const bpb = 8 * sizeof(uint64_t);
  34. for (size_t nblocks = 1; nblocks <= maxblocks; ++nblocks) {
  35. small_vector<uint64_t, maxblocks> data(nblocks, 0);
  36. size_t nbits = nblocks * bpb;
  37. auto begin = makeBitIterator(data.cbegin());
  38. auto end = makeBitIterator(data.cend());
  39. DCHECK_EQ(nbits, end - begin);
  40. DCHECK(begin != end);
  41. // Try every possible combination of first bit set (including none),
  42. // start bit, end bit
  43. for (size_t firstSet = 0; firstSet <= nbits; ++firstSet) {
  44. data.assign(nblocks, 0);
  45. if (firstSet) {
  46. size_t b = firstSet - 1;
  47. data[b / bpb] |= (1ULL << (b % bpb));
  48. }
  49. for (size_t startBit = 0; startBit <= nbits; ++startBit) {
  50. for (size_t endBit = startBit; endBit <= nbits; ++endBit) {
  51. auto p = begin + startBit;
  52. auto q = begin + endBit;
  53. p = fn(p, q);
  54. doNotOptimizeAway(p);
  55. if (firstSet < startBit + 1 || firstSet >= endBit + 1) {
  56. DCHECK_EQ(endBit, p - begin)
  57. << " firstSet=" << firstSet << " startBit=" << startBit
  58. << " endBit=" << endBit << " nblocks=" << nblocks;
  59. } else {
  60. DCHECK_EQ(firstSet - 1, p - begin)
  61. << " firstSet=" << firstSet << " startBit=" << startBit
  62. << " endBit=" << endBit << " nblocks=" << nblocks;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. void runSimpleFFSTest(int iters) {
  70. while (iters--) {
  71. runFFSTest([](auto first, auto last) { return simpleFFS(first, last); });
  72. }
  73. }
  74. void runRealFFSTest(int iters) {
  75. while (iters--) {
  76. runFFSTest([](auto first, auto last) { return findFirstSet(first, last); });
  77. }
  78. }
  79. } // namespace
  80. BENCHMARK(SimpleFFSTest, iters) {
  81. runSimpleFFSTest(iters);
  82. }
  83. BENCHMARK(RealFFSTest, iters) {
  84. runRealFFSTest(iters);
  85. }
  86. /* --bm_min_iters=10 --bm_max_iters=100
  87. Benchmark Iters Total t t/iter iter/sec
  88. ------------------------------------------------------------------------------
  89. runSimpleFFSTest 10 4.82 s 482 ms 2.075
  90. runRealFFSTest 19 2.011 s 105.9 ms 9.447
  91. */
  92. int main(int argc, char** argv) {
  93. folly::init(&argc, &argv);
  94. folly::runBenchmarks();
  95. return 0;
  96. }