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 **********************************************************************/
7 #ifndef SECP256K1_BENCH_H
8 #define SECP256K1_BENCH_H
15 static int64_t gettime_i64(void) {
17 gettimeofday(&tv, NULL);
18 return (int64_t)tv.tv_usec + (int64_t)tv.tv_sec * 1000000LL;
22 #define FP_MULT (1000000LL)
24 /* Format fixed point number. */
25 void print_number(const int64_t x) {
36 x_abs = x < 0 ? -x : x;
38 /* Determine how many decimals we want to show (more than FP_EXP makes no
42 while (y > 0LL && y < 100LL * FP_MULT && c < FP_EXP) {
47 /* Round to 'c' decimals. */
50 for (i = c; i < FP_EXP; ++i) {
51 rounding = (y % 10) >= 5;
56 /* Format and print the number. */
57 ptr = sizeof(buffer) - 1;
60 for (i = 0; i < c; ++i) {
61 buffer[--ptr] = '0' + (y % 10);
67 buffer[--ptr] = '0' + (y % 10);
73 printf("%s", &buffer[ptr]);
76 void run_benchmark(char *name, void (*benchmark)(void*, int), void (*setup)(void*), void (*teardown)(void*, int), void* data, int count, int iter) {
78 int64_t min = INT64_MAX;
81 for (i = 0; i < count; i++) {
86 begin = gettime_i64();
87 benchmark(data, iter);
88 total = gettime_i64() - begin;
89 if (teardown != NULL) {
100 printf("%s: min ", name);
101 print_number(min * FP_MULT / iter);
103 print_number(((sum * FP_MULT) / count) / iter);
105 print_number(max * FP_MULT / iter);
109 int have_flag(int argc, char** argv, char *flag) {
110 char** argm = argv + argc;
115 while (argv != NULL && argv != argm) {
116 if (strcmp(*argv, flag) == 0) {
124 int get_iters(int default_iters) {
125 char* env = getenv("SECP256K1_BENCH_ITERS");
127 return strtol(env, NULL, 0);
129 return default_iters;
133 #endif /* SECP256K1_BENCH_H */