]>
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 | ||
20 | void run_benchmark(void (*benchmark)(void*), void (*setup)(void*), void (*teardown)(void*), void* data, int count, int iter) { | |
21 | double min = HUGE_VAL; | |
22 | double sum = 0.0; | |
23 | double max = 0.0; | |
24 | for (int i = 0; i < count; i++) { | |
25 | if (setup) setup(data); | |
26 | double begin = gettimedouble(); | |
27 | benchmark(data); | |
28 | double total = gettimedouble() - begin; | |
29 | if (teardown) teardown(data); | |
30 | if (total < min) min = total; | |
31 | if (total > max) max = total; | |
32 | sum += total; | |
33 | } | |
34 | printf("min %.3fus / avg %.3fus / max %.3fus\n", min * 1000000.0 / iter, (sum / count) * 1000000.0 / iter, max * 1000000.0 / iter); | |
35 | } | |
36 | ||
37 | #endif |