go-ethereum/crypto/secp256k1/libsecp256k1/src/bench.h

67 lines
1.7 KiB
C
Raw Normal View History

2015-04-07 05:13:37 -05:00
/**********************************************************************
* Copyright (c) 2014 Pieter Wuille *
* Distributed under the MIT software license, see the accompanying *
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
**********************************************************************/
#ifndef _SECP256K1_BENCH_H_
#define _SECP256K1_BENCH_H_
#include <stdio.h>
#include <math.h>
#include "sys/time.h"
static double gettimedouble(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec * 0.000001 + tv.tv_sec;
}
void print_number(double x) {
double y = x;
int c = 0;
2015-09-28 10:46:17 -05:00
if (y < 0.0) {
y = -y;
}
2015-04-07 05:13:37 -05:00
while (y < 100.0) {
y *= 10.0;
c++;
}
printf("%.*f", c, x);
}
void run_benchmark(char *name, void (*benchmark)(void*), void (*setup)(void*), void (*teardown)(void*), void* data, int count, int iter) {
int i;
double min = HUGE_VAL;
double sum = 0.0;
double max = 0.0;
for (i = 0; i < count; i++) {
double begin, total;
2015-09-28 10:46:17 -05:00
if (setup != NULL) {
setup(data);
}
2015-04-07 05:13:37 -05:00
begin = gettimedouble();
benchmark(data);
total = gettimedouble() - begin;
2015-09-28 10:46:17 -05:00
if (teardown != NULL) {
teardown(data);
}
if (total < min) {
min = total;
}
if (total > max) {
max = total;
}
2015-04-07 05:13:37 -05:00
sum += total;
}
printf("%s: min ", name);
print_number(min * 1000000.0 / iter);
printf("us / avg ");
print_number((sum / count) * 1000000.0 / iter);
2015-09-28 10:46:17 -05:00
printf("us / max ");
2015-04-07 05:13:37 -05:00
print_number(max * 1000000.0 / iter);
printf("us\n");
}
#endif