proxygen
RandomBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-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 
17 #include <folly/Random.h>
18 
19 #include <random>
20 #include <thread>
21 
22 #include <glog/logging.h>
23 
24 #include <folly/Benchmark.h>
26 
27 #if FOLLY_HAVE_EXTRANDOM_SFMT19937
28 #include <ext/random>
29 #endif
30 
31 using namespace folly;
32 
33 BENCHMARK(minstdrand, n) {
34  BenchmarkSuspender braces;
35  std::random_device rd;
36  std::minstd_rand rng(rd());
37 
38  braces.dismiss();
39 
40  FOR_EACH_RANGE (i, 0, n) { doNotOptimizeAway(rng()); }
41 }
42 
43 BENCHMARK(mt19937, n) {
44  BenchmarkSuspender braces;
45  std::random_device rd;
46  std::mt19937 rng(rd());
47 
48  braces.dismiss();
49 
50  FOR_EACH_RANGE (i, 0, n) { doNotOptimizeAway(rng()); }
51 }
52 
53 #if FOLLY_HAVE_EXTRANDOM_SFMT19937
54 BENCHMARK(sfmt19937, n) {
55  BenchmarkSuspender braces;
56  std::random_device rd;
57  __gnu_cxx::sfmt19937 rng(rd());
58 
59  braces.dismiss();
60 
61  FOR_EACH_RANGE (i, 0, n) { doNotOptimizeAway(rng()); }
62 }
63 #endif
64 
65 BENCHMARK(threadprng, n) {
66  BenchmarkSuspender braces;
67  ThreadLocalPRNG tprng;
68  tprng();
69 
70  braces.dismiss();
71 
72  FOR_EACH_RANGE (i, 0, n) { doNotOptimizeAway(tprng()); }
73 }
74 
75 BENCHMARK(RandomDouble) {
77 }
78 BENCHMARK(Random32) {
80 }
81 BENCHMARK(Random32Num) {
83 }
84 BENCHMARK(Random64) {
86 }
87 BENCHMARK(Random64Num) {
88  doNotOptimizeAway(Random::rand64(100ull << 32));
89 }
90 BENCHMARK(Random64OneIn) {
92 }
93 
94 int main(int argc, char** argv) {
95  gflags::ParseCommandLineFlags(&argc, &argv, true);
97  return 0;
98 }
static bool oneIn(uint32_t n)
Definition: Random.h:311
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void runBenchmarks()
Definition: Benchmark.cpp:456
auto rng
Definition: CollectTest.cpp:31
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
char ** argv
int main(int argc, char **argv)
static double randDouble01()
Definition: Random.h:329
std::random_device rd
BENCHMARK(fbFollyGlobalBenchmarkBaseline)
Definition: Benchmark.cpp:84
static uint32_t rand32()
Definition: Random.h:213
static uint64_t rand64()
Definition: Random.h:263
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258