ParallelMapBenchmark.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2014-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 <algorithm>
  17. #include <atomic>
  18. #include <thread>
  19. #include <vector>
  20. #include <folly/Benchmark.h>
  21. #include <folly/gen/Base.h>
  22. #include <folly/gen/ParallelMap.h>
  23. #include <folly/portability/Unistd.h>
  24. using namespace folly::gen;
  25. DEFINE_int32(
  26. threads,
  27. std::max(1, (int32_t)sysconf(_SC_NPROCESSORS_CONF) / 2),
  28. "Num threads.");
  29. constexpr int kFib = 35; // unit of work
  30. size_t fib(int n) {
  31. return n <= 1 ? 1 : fib(n - 1) * fib(n - 2);
  32. }
  33. BENCHMARK(FibSumMap, n) {
  34. auto result = seq(1, (int)n) | map([](int) { return fib(kFib); }) | sum;
  35. folly::doNotOptimizeAway(result);
  36. }
  37. BENCHMARK_RELATIVE(FibSumPmap, n) {
  38. // Schedule more work: enough so that each worker thread does the
  39. // same amount as one FibSumMap.
  40. const size_t kNumThreads = FLAGS_threads;
  41. // clang-format off
  42. auto result =
  43. seq(1, (int)(n * kNumThreads))
  44. | pmap([](int) { return fib(kFib); }, kNumThreads)
  45. | sum;
  46. // clang-format on
  47. folly::doNotOptimizeAway(result);
  48. }
  49. BENCHMARK_RELATIVE(FibSumThreads, n) {
  50. // Schedule kNumThreads to execute the same code as FibSumMap.
  51. const size_t kNumThreads = FLAGS_threads;
  52. std::vector<std::thread> workers;
  53. workers.reserve(kNumThreads);
  54. auto fn = [n] {
  55. auto result = seq(1, (int)n) | map([](int) { return fib(kFib); }) | sum;
  56. folly::doNotOptimizeAway(result);
  57. };
  58. for (size_t i = 0; i < kNumThreads; i++) {
  59. workers.push_back(std::thread(fn));
  60. }
  61. for (auto& w : workers) {
  62. w.join();
  63. }
  64. }
  65. /*
  66. ============================================================================
  67. folly/gen/test/ParallelMapBenchmark.cpp relative time/iter iters/s
  68. ============================================================================
  69. FibSumMap 41.64ms 24.02
  70. FibSumPmap 98.38% 42.32ms 23.63
  71. FibSumThreads 94.48% 44.07ms 22.69
  72. ============================================================================
  73. real0m15.595s
  74. user2m47.100s
  75. sys0m0.016s
  76. */
  77. int main(int argc, char* argv[]) {
  78. gflags::ParseCommandLineFlags(&argc, &argv, true);
  79. folly::runBenchmarks();
  80. return 0;
  81. }