Fabcoin Core  0.16.2
P2P Digital Currency
bench.h
Go to the documentation of this file.
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 print_number(double x) {
21  double y = x;
22  int c = 0;
23  if (y < 0.0) {
24  y = -y;
25  }
26  while (y > 0 && y < 100.0) {
27  y *= 10.0;
28  c++;
29  }
30  printf("%.*f", c, x);
31 }
32 
33 void run_benchmark(char *name, void (*benchmark)(void*), void (*setup)(void*), void (*teardown)(void*), void* data, int count, int iter) {
34  int i;
35  double min = HUGE_VAL;
36  double sum = 0.0;
37  double max = 0.0;
38  for (i = 0; i < count; i++) {
39  double begin, total;
40  if (setup != NULL) {
41  setup(data);
42  }
43  begin = gettimedouble();
44  benchmark(data);
45  total = gettimedouble() - begin;
46  if (teardown != NULL) {
47  teardown(data);
48  }
49  if (total < min) {
50  min = total;
51  }
52  if (total > max) {
53  max = total;
54  }
55  sum += total;
56  }
57  printf("%s: min ", name);
58  print_number(min * 1000000.0 / iter);
59  printf("us / avg ");
60  print_number((sum / count) * 1000000.0 / iter);
61  printf("us / max ");
62  print_number(max * 1000000.0 / iter);
63  printf("us\n");
64 }
65 
66 #endif /* SECP256K1_BENCH_H */
void printf(const char *fmt, const Args &...args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:972
size_t count
Definition: ExecStats.cpp:37
#define c(i)
ExecStats::duration min
Definition: ExecStats.cpp:35
void run_benchmark(char *name, void(*benchmark)(void *), void(*setup)(void *), void(*teardown)(void *), void *data, int count, int iter)
Definition: bench.h:33
#define x(i)
void print_number(double x)
Definition: bench.h:20
const char * name
Definition: rest.cpp:36
ExecStats::duration max
Definition: ExecStats.cpp:36
volatile double sum
Definition: Examples.cpp:23
uint8_t const * data
Definition: sha3.h:19