2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/Random.h>
19 #include <folly/Benchmark.h>
20 #include <folly/Foreach.h>
21 #include <folly/Range.h>
23 #include <glog/logging.h>
28 using namespace folly;
30 BENCHMARK(minstdrand, n) {
31 BenchmarkSuspender braces;
32 std::random_device rd;
33 std::minstd_rand rng(rd());
37 FOR_EACH_RANGE(i, 0, n) { doNotOptimizeAway(rng()); }
40 BENCHMARK(mt19937, n) {
41 BenchmarkSuspender braces;
42 std::random_device rd;
43 std::mt19937 rng(rd());
47 FOR_EACH_RANGE(i, 0, n) { doNotOptimizeAway(rng()); }
50 BENCHMARK(threadprng, n) {
51 BenchmarkSuspender braces;
52 ThreadLocalPRNG tprng;
57 FOR_EACH_RANGE(i, 0, n) { doNotOptimizeAway(tprng()); }
60 BENCHMARK(RandomDouble) { doNotOptimizeAway(Random::randDouble01()); }
61 BENCHMARK(Random32) { doNotOptimizeAway(Random::rand32()); }
62 BENCHMARK(Random32Num) { doNotOptimizeAway(Random::rand32(100)); }
63 BENCHMARK(Random64) { doNotOptimizeAway(Random::rand64()); }
64 BENCHMARK(Random64Num) { doNotOptimizeAway(Random::rand64(100ull << 32)); }
65 BENCHMARK(Random64OneIn) { doNotOptimizeAway(Random::oneIn(100)); }
67 int main(int argc, char** argv) {
68 gflags::ParseCommandLineFlags(&argc, &argv, true);
69 folly::runBenchmarks();