-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance.cpp
More file actions
122 lines (87 loc) · 3.96 KB
/
performance.cpp
File metadata and controls
122 lines (87 loc) · 3.96 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <iomanip>
#include <random>
#include <chrono>
#include <cassert>
#include "random.h"
#include "timer.h"
using namespace std;
template<class G>
void single_run (G& generator, size_t times ) {
typename G::result_type total = 0;
for ( size_t i = 0; i < times; ++i )
total += generator();
if ( total == 0 ) cout << "This is to avoid compiler optimizations." << endl;
}
template<class G>
double average_time(G& generator, size_t trials, size_t times ) {
double average_time = 0.0;
for ( size_t i = 0; i < trials; ++i ) {
timer elapsed;
single_run( generator, times );
average_time += elapsed.elapsed();
}
return trials * times / average_time / 1000000.0;
}
template<class Real, class G>
void canonical_single_run (G& generator, size_t times ) {
Real total = 0.0;
for ( size_t i = 0; i < times; ++i )
total += generate_canonical<Real, sizeof(Real)>(generator);
if ( total == 0 ) cout << "This is to avoid compiler optimizations." << endl;
}
template<class Real, class G>
double canonical_average_time(G& generator, size_t trials, size_t times ) {
double average_time = 0.0;
for ( size_t i = 0; i < trials; ++i ) {
timer elapsed;
canonical_single_run<Real>( generator, times );
average_time += elapsed.elapsed();
}
return trials * times / average_time / 1000000.0;
}
template<class Real, class G, class D>
void dist_single_run (D& dist, G& generator, size_t times ) {
Real total = 0.0;
for ( size_t i = 0; i < times; ++i )
total += dist(generator);
if ( total == 0 ) cout << "This is to avoid compiler optimizations." << endl;
}
template<class Real, class G, class D>
double dist_average_time(D& dist, G& generator, size_t trials, size_t times ) {
double average_time = 0.0;
for ( size_t i = 0; i < trials; ++i ) {
timer elapsed;
dist_single_run<Real>( dist, generator, times );
average_time += elapsed.elapsed();
}
return trials * times / average_time / 1000000.0;
}
int main ( ) {
typedef double Real;
cout.precision(2);
random_device rd;
size_t trials = 5;
size_t times = 100000000;
cout << "Testing base generators..." << endl;
xorshf xorgen( rd() );
cout << "Generator: xorshf\tspeed: " << fixed << average_time(xorgen, trials, times) << " M/s" << endl;
xorshf64 xorgen64( rd() );
cout << "Generator: xorshf64\tspeed: " << fixed << average_time(xorgen64, trials, times) << " M/s" << endl;
mt19937 mtgen( rd() );
cout << "Generator: mt19937\tspeed: " << fixed << average_time(mtgen, trials, times) << " M/s" << endl;
minstd_rand minstdgen( rd() );
cout << "Generator: minstd\tspeed: " << fixed << average_time(minstdgen, trials, times) << " M/s" << endl;
cout << endl << "Testing canonical [0,1) distribution..." << endl;
cout << "Generator: xorshf\tspeed: " << fixed << canonical_average_time<Real>(xorgen, trials, times) << " M/s" << endl;
cout << "Generator: xorshf64\tspeed: " << fixed << canonical_average_time<Real>(xorgen64, trials, times) << " M/s" << endl;
cout << "Generator: mt19937\tspeed: " << fixed << canonical_average_time<Real>(mtgen, trials, times) << " M/s" << endl;
cout << "Generator: minstd\tspeed: " << fixed << canonical_average_time<Real>(minstdgen, trials, times) << " M/s" << endl;
uniform_real_distribution<> dist(-1, 1);
cout << endl << "Testing [-1,1) distribution..." << endl;
cout << "Generator: xorshf\tspeed: " << fixed << dist_average_time<Real>(dist, xorgen, trials, times) << " M/s" << endl;
cout << "Generator: xorshf64\tspeed: " << fixed << dist_average_time<Real>(dist, xorgen64, trials, times) << " M/s" << endl;
cout << "Generator: mt19937\tspeed: " << fixed << dist_average_time<Real>(dist, mtgen, trials, times) << " M/s" << endl;
cout << "Generator: minstd\tspeed: " << fixed << dist_average_time<Real>(dist, minstdgen, trials, times) << " M/s" << endl;
return 0;
}