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