]>
Commit | Line | Data |
---|---|---|
6558a267 PW |
1 | /********************************************************************** |
2 | * Copyright (c) 2014 Pieter Wuille * | |
3 | * Distributed under the MIT software license, see the accompanying * | |
4 | * file COPYING or http://www.opensource.org/licenses/mit-license.php.* | |
5 | **********************************************************************/ | |
6 | ||
7 | #ifndef _SECP256K1_BENCH_H_ | |
8 | #define _SECP256K1_BENCH_H_ | |
9 | ||
10 | #include <stdio.h> | |
11 | #include <math.h> | |
12 | #include "sys/time.h" | |
13 | ||
14 | static double gettimedouble(void) { | |
15 | struct timeval tv; | |
16 | gettimeofday(&tv, NULL); | |
17 | return tv.tv_usec * 0.000001 + tv.tv_sec; | |
18 | } | |
19 | ||
039723d5 PW |
20 | void print_number(double x) { |
21 | double y = x; | |
22 | int c = 0; | |
23 | if (y < 0.0) y = -y; | |
24 | while (y < 100.0) { | |
25 | y *= 10.0; | |
26 | c++; | |
27 | } | |
28 | printf("%.*f", c, x); | |
29 | } | |
30 | ||
31 | void run_benchmark(char *name, void (*benchmark)(void*), void (*setup)(void*), void (*teardown)(void*), void* data, int count, int iter) { | |
f735446c | 32 | int i; |
6558a267 PW |
33 | double min = HUGE_VAL; |
34 | double sum = 0.0; | |
35 | double max = 0.0; | |
f735446c GM |
36 | for (i = 0; i < count; i++) { |
37 | double begin, total; | |
6558a267 | 38 | if (setup) setup(data); |
f735446c | 39 | begin = gettimedouble(); |
6558a267 | 40 | benchmark(data); |
f735446c | 41 | total = gettimedouble() - begin; |
6558a267 PW |
42 | if (teardown) teardown(data); |
43 | if (total < min) min = total; | |
44 | if (total > max) max = total; | |
45 | sum += total; | |
46 | } | |
039723d5 PW |
47 | printf("%s: min ", name); |
48 | print_number(min * 1000000.0 / iter); | |
49 | printf("us / avg "); | |
50 | print_number((sum / count) * 1000000.0 / iter); | |
6066bb68 | 51 | printf("us / max "); |
039723d5 PW |
52 | print_number(max * 1000000.0 / iter); |
53 | printf("us\n"); | |
6558a267 PW |
54 | } |
55 | ||
56 | #endif |