forked from camel-cdr/cauldron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.h
230 lines (197 loc) · 5.82 KB
/
bench.h
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* bench.h -- A minimal benchmarking library
* Olaf Bernstein <[email protected]>
* Distributed under the MIT license, see license at the end of the file.
* New versions available at https://github.com/camel-cdr/cauldron
*/
#ifndef BENCH_H_INCLUDED
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <float.h>
#include <math.h>
#if !defined(__pnacl__) && !defined(__EMSCRIPTEN__) && \
(defined(__clang__) || defined(__GNUC__) || defined(__INTEL_COMPILER))
/* artificial use of all of memory */
# define BENCH_CLOBBER() asm volatile("":::"memory")
/* artificial dependency of x on all of memory and all of memory on x */
# define BENCH_VOLATILE(x) asm volatile("" : "+g"(x) : "g"(x) : "memory")
# define BENCH_VOLATILE_REG(x) asm volatile("" : "+r"(x) : "r"(x) : "memory")
# define BENCH_VOLATILE_MEM(x) asm volatile("" : "+m"(x) : "m"(x) : "memory")
#else
# if defined(_MSC_VER)
# pragma optimize("", off)
static inline void bench__use_ptr(void const volatile *x) {}
/* artificial use of all of memory */
# define BENCH_CLOBBER() _ReadWriteBarrier()
# pragma optimize("", on)
# else
static void bench_use_ptr(char const volatile *x) {}
/* artificial use of all of memory */
# define BENCH_CLOBBER()
# endif
/* artificial use of all of memory dependent on x */
# define BENCH_CLOBBER_WITH(x) (bench__use_ptr(&(x)), BENCH_CLOBBER())
# define BENCH_CLOBBER_WITH_REG(x) (bench__use_ptr(&(x)), BENCH_CLOBBER())
# define BENCH_CLOBBER_WITH_MEM(x) (bench__use_ptr(&(x)), BENCH_CLOBBER())
#endif
typedef struct {
size_t count;
double min, mean, M2;
char const *title;
} BenchRecord;
typedef struct {
size_t count, cap;
BenchRecord *records;
/* temporaries */
size_t i;
double secs;
} Bench;
static Bench benchInternal;
#define BENCH(title, warmup, samples) \
for (bench_append(title), \
benchInternal.i = (warmup) + (samples); \
(benchInternal.secs = bench_gettime()), benchInternal.i--; \
benchInternal.i < (samples) ? \
bench_update(bench_gettime()-benchInternal.secs),0 : 0)
static inline double
bench_gettime(void)
{
#if defined(CLOCK_PROCESS_CPUTIME_ID)
struct timespec t;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t);
return t.tv_nsec * 1.0/1000000000 + t.tv_sec;
#elif defined(CLOCK_MONOTONIC_RAW)
struct timespec t;
clock_gettime(CLOCK_MONOTONIC_RAW, &t);
return t.tv_nsec * 1.0/1000000000 + t.tv_sec;
#else
return clock() * 1.0 / CLOCKS_PER_SEC;
#endif
}
static inline void
bench_append(char const *title)
{
Bench *b = &benchInternal;
BenchRecord *r;
if (b->count >= b->cap) {
b->cap = (b->cap << 1) + 1;
b->records = (BenchRecord *)
realloc(b->records, b->cap * sizeof *b->records);
}
r = &b->records[b->count++];
r->mean = r->M2 = r->count = 0;
r->min = DBL_MAX;
r->title = title;
}
static inline void
bench_update(double time)
{
BenchRecord *r = &benchInternal.records[benchInternal.count-1];
double const delta = time - r->mean;
r->mean += delta / ++r->count;
r->M2 += delta * (time - r->mean);
if (time < r->min)
r->min = time;
}
static inline int
bench_record_cmp(void const *lhs, void const *rhs)
{
BenchRecord const *l = (BenchRecord const *)lhs;
BenchRecord const *r = (BenchRecord const *)rhs;
return l->mean > r->mean ? 1 : -1;
}
static inline void
bench_done(void)
{
size_t i, j, maxlen;
double minmean = DBL_MAX;
Bench *b = &benchInternal;
qsort(b->records, b->count, sizeof *b->records, bench_record_cmp);
for (maxlen = i = 0; i < b->count; ++i) {
size_t l = strlen(b->records[i].title);
if (l > maxlen)
maxlen = l;
if (b->records[i].mean < minmean)
minmean = b->records[i].mean;
}
for (i = 0; i < b->count; ++i) {
int l = printf("%s:", b->records[i].title) - 4;
for (j = 0; j < maxlen-l; ++j)
putchar(' ');
printf("mean: %.9e, stddev: %.2e, min: %.9e \n",
#ifdef BENCH_DONT_NORMALIZE
b->records[i].mean,
sqrt(b->records[i].M2 / b->records[i].count),
#else
b->records[i].mean / minmean,
sqrt(b->records[i].M2 / b->records[i].count) / minmean,
#endif
b->records[i].min);
}
b->count = 0;
}
static inline void
bench_free(void)
{
free(benchInternal.records);
}
static inline uint64_t
bench_hash64(uint64_t x)
{
x ^= x >> 30;
x *= 0xBF58476D1CE4E5B9u;
x ^= x >> 27;
x *= 0x94D049BB133111EBu;
x ^= x >> 31;
return x;
}
#define BENCH_H_INCLUDED
#endif
/*
* Example:
*/
#ifdef BENCH_EXAMPLE
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
size_t i;
BENCH("sum", 8, 64) {
unsigned int sum = 0;
for (i = 0; i < 1024*16u; ++i) {
sum += i;
BENCH_VOLATILE_REG(sum);
}
}
BENCH("product", 8, 64) {
unsigned char sum = 0;
for (i = 0; i < 1024*16u; ++i) {
sum *= i;
BENCH_VOLATILE_REG(sum);
}
}
bench_done();
return 0;
}
#endif /* BENCH_EXAMPLE */
/*
* Copyright (c) 2022 Olaf Berstein
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/