-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.c
70 lines (49 loc) · 1.08 KB
/
benchmark.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
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <windows.h>
double time()
{
LARGE_INTEGER t, f;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&f);
return (double)t.QuadPart * 1000000 / (double)f.QuadPart;
}
void benchmark(void* function(), char *id, unsigned long long count, int progress)
{
double* times = malloc(sizeof(double) * count), total = 0, mean, sd = 0, best = DBL_MAX, worst = 0;
printf("#%s\n", id);
for (int i = 0; i < count; i++)
{
double current = time();
function();
current = time() - current;
times[i] = current;
total += current;
if (current > worst)
{
worst = current;
}
if (current < best)
{
best = current;
}
if (progress)
{
printf("[%04d] %.3fus\n", i + 1, times[i]);
}
}
if (total)
{
mean = total / count;
for (int i = 0; i < count; i++)
{
sd += pow(times[i] - mean, 2);
}
printf("Mean: %.3fus\n", mean);
printf("SD: %.3fus\n", sqrt(sd / count));
printf("Best: %.3fus\n", best);
printf("Worst: %.3fus\n", worst);
}
printf("------------------------------\n");
}