-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_prng.cpp
61 lines (54 loc) · 1.56 KB
/
test_prng.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <vb/util/PRNG.h>
#include <vb/util/misc.h>
using namespace vb;
using namespace std;
auto main(int argc, char **argv) -> int {
Hub H("Testing PRNG methods", argc, argv, "n=100000000");
int n = H['n'];
string state = prng.state();
timing(H, "vb::PRNG::gaussian", [n] {
double s = 0;
for (int i = 0; i < n; ++i) {
double o = prng.gaussian();
s += o * o;
}
return s / n;
});
timing(H, "boost::gaussian_distribution", [n] {
boost::normal_distribution<> dist(0, 1);
boost::mt19937_64 boostengine;
double s = 0;
for (int i = 0; i < n; ++i) {
double o = dist(boostengine);
s += o * o;
}
return s / n;
});
timing(H, "many boost::gaussian_distributions", [n] {
double s = 0;
for (int i = 0; i < n; ++i) {
double o = (boost::normal_distribution<>(0, 1))(prng);
s += o * o;
}
return s / n;
});
timing(H, "C++11 style", [n] {
std::normal_distribution<> dist2(0, 1);
std::mt19937_64 engine;
double s = 0;
for (int i = 0; i < n; ++i) {
double o = dist2(engine);
s += o * o;
}
return s / n;
});
prng.state(state);
timing(H, "Stored state, first version", [n] {
double s = 0;
for (int i = 0; i < n; ++i) {
double o = prng.gaussian();
s += o * o;
}
return s / n;
});
}