-
Notifications
You must be signed in to change notification settings - Fork 7
/
bench.c
157 lines (133 loc) · 3.24 KB
/
bench.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#define RANDOM_H_IMPLEMENTATION
#include <cauldron/random.h>
#include <cauldron/bench.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "extra.h"
#define COUNT (1024*512*2ull)
#define SAMPLES (64*2)
#define BENCH_RNG(name, type, init, next, ftype, max) \
do { \
type rng; \
init; \
BENCH(name, 8, SAMPLES) { \
ftype x, y; \
size_t i, c; \
double pi; \
for (i = c = 0; i < COUNT; ++i) { \
x = next * (ftype)1.0 / max; \
y = next * (ftype)1.0 / max; \
if (x*x + y*y <= (ftype)1.0) \
++c; \
} \
pi = (double)c / COUNT * 4.0; \
BENCH_VOLATILE(pi); \
} \
} while (0);
static void
bench_rng_16(void)
{
puts("16-bit PRNGs:");
#define RANDOM_X16(type, func, rnd) \
BENCH_RNG(#func, type, rnd(&rng), func(&rng), float, UINT16_MAX)
#define RANDOM_X32(type, func, rnd)
#define RANDOM_X64(type, func, rnd)
#include <cauldron/random-xmacros.h>
#include "extra-xmacros.h"
#undef RANDOM_X16
#undef RANDOM_X32
#undef RANDOM_X64
bench_done();
putchar('\n');
}
static void
bench_rng_32(void)
{
puts("32-bit PRNGs");
#define RANDOM_X16(type, func, rnd)
#define RANDOM_X32(type, func, rnd) \
BENCH_RNG(#func, type, rnd(&rng), func(&rng), float, UINT32_MAX)
#define RANDOM_X64(type, func, rnd)
#include <cauldron/random-xmacros.h>
#include "extra-xmacros.h"
#undef RANDOM_X16
#undef RANDOM_X32
#undef RANDOM_X64
bench_done();
putchar('\n');
}
static void
bench_rng_64(void)
{
puts("64-bit PRNGs");
#define RANDOM_X16(type, func, rnd)
#define RANDOM_X32(type, func, rnd)
#define RANDOM_X64(type, func, rnd) \
BENCH_RNG(#func, type, rnd(&rng), func(&rng), double, UINT64_MAX)
#include <cauldron/random-xmacros.h>
#include "extra-xmacros.h"
#undef RANDOM_X16
#undef RANDOM_X32
#undef RANDOM_X64
bench_done();
putchar('\n');
}
#define BENCH_NORM_IMPL(name, type, init, next, ftype) \
do { \
type rng; \
init(&rng); \
BENCH(name, 8, SAMPLES) { \
ftype x, y; \
size_t i, c; \
for (i = c = 0; i < COUNT; ++i) { \
x = next; \
y = next; \
if (x*x + y*y <= 1) \
++c; \
} \
BENCH_VOLATILE(c); \
} \
} while (0);
#define NORM_NEXT prng64_romu_duo_jr
#define BENCH_NORM(name, next) BENCH_NORM_IMPL( \
name, PRNG64RomuDuo, prng64_romu_duo_randomize, next, double)
#define NORMF_NEXT prng32_romu_trio
#define BENCH_NORMF(name, next) BENCH_NORM_IMPL( \
name, PRNG32RomuTrio, prng32_romu_trio_randomize, next, float)
static void
bench_normal(void)
{
DistNormalZig zig;
dist_normal_zig_init(&zig);
puts("normal distribution using prng64_romu_duo_jr");
BENCH_NORM("dist_normalf_fast", dist_normalf_fast(NORM_NEXT(&rng)));
BENCH_NORM("dist_normal", dist_normal(NORM_NEXT, &rng));
BENCH_NORM("dist_normal_zig", dist_normal_zig(&zig, NORM_NEXT, &rng));
bench_done();
putchar('\n');
}
static void
bench_normalf(void)
{
DistNormalfZig zig;
dist_normalf_zig_init(&zig);
puts("normal distribution using prng32_romu_trio");
BENCH_NORMF("dist_normalf", dist_normalf(NORMF_NEXT, &rng));
BENCH_NORMF("dist_normalf_zig", dist_normalf_zig(&zig, NORMF_NEXT, &rng));
bench_done();
putchar('\n');
}
int
main(void)
{
size_t i;
puts("Note: Execution times between categories aren't comparable!\n");
bench_rng_16();
bench_rng_32();
bench_rng_64();
bench_normal();
bench_normalf();
bench_free();
return 0;
}