-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistribution.cpp
More file actions
73 lines (66 loc) · 2.43 KB
/
distribution.cpp
File metadata and controls
73 lines (66 loc) · 2.43 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
#include <cstdio>
#include "timer.h"
#include <random>
#include <cmath>
#include <ctime>
#define SAMPLE_SIZE 100000
#define TRIALS 5
std::normal_distribution<double> get_normal_distribution() {
std::normal_distribution<> d(0,1);
return d;
}
std::bernoulli_distribution get_bernoulli_distribution() {
std::bernoulli_distribution d(0.5);
return d;
}
std::poisson_distribution<int> get_poisson_distribution() {
std::poisson_distribution<> d(4);
return d;
}
std::exponential_distribution<double> get_exponential_distribution() {
std::exponential_distribution<> d(1);
return d;
}
std::discrete_distribution<int> get_discrete_distribution() {
std::discrete_distribution<> d({40, 10, 10, 40});
return d;
}
template <class T>
long benchmark(T dist, std::mt19937 mt_rand) {
CUtilTimer timer;
timer.start();
std::clock_t start;
for(int i=0;i<TRIALS;i++) {
for(int j=0;j<SAMPLE_SIZE;j++) dist(mt_rand);
}
timer.stop();
return timer.get_ticks()/TRIALS;
}
long benchmarkmt19937(std::mt19937 mt_rand) {
CUtilTimer timer;
std::clock_t start;
timer.start();
for(int i=0;i<TRIALS;i++) {
for(int j=0;j<SAMPLE_SIZE;j++) mt_rand();
}
timer.stop();
return timer.get_ticks()/TRIALS;
}
int main() {
std::mt19937 mt_rand(time(nullptr));
std::printf("%-30s %-15s %-15s %-15s\n","Distribution","Sample Size","Clocks","Clocks per Sample");
long avgcyc = benchmark(get_normal_distribution(), mt_rand);
std::printf("%-30s %-15d %-15ld %-15ld\n","Normal Distribution",SAMPLE_SIZE,avgcyc,(long)std::round((double)avgcyc/SAMPLE_SIZE));
avgcyc = benchmark(get_bernoulli_distribution(), mt_rand);
std::printf("%-30s %-15d %-15ld %-15ld\n","Bernoulli Distribution",SAMPLE_SIZE,avgcyc,(long)std::round((double)avgcyc/SAMPLE_SIZE));
avgcyc = benchmark(get_poisson_distribution(), mt_rand);
std::printf("%-30s %-15d %-15ld %-15ld\n","Poisson Distribution",SAMPLE_SIZE,avgcyc,(long)std::round((double)avgcyc/SAMPLE_SIZE));
avgcyc = benchmark(get_exponential_distribution(), mt_rand);
std::printf("%-30s %-15d %-15ld %-15ld\n","Exponential Distribution",SAMPLE_SIZE,avgcyc,(long)std::round((double)avgcyc/SAMPLE_SIZE));
avgcyc = benchmark(get_discrete_distribution(), mt_rand);
std::printf("%-30s %-15d %-15ld %-15ld\n","Discrete Distribution",SAMPLE_SIZE,avgcyc,(long)std::round((double)avgcyc/SAMPLE_SIZE));
std::printf("\n");
avgcyc = benchmarkmt19937(mt_rand);
std::printf("Clock Cycles per Call to mt19937: %ld\n", (long)std::round((double)avgcyc/SAMPLE_SIZE));
return 0;
}