github.com/aquanetwork/aquachain@v1.7.8/crypto/secp256k1/libsecp256k1/src/bench.h (about)

     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 <string.h>
    12  #include <math.h>
    13  #include "sys/time.h"
    14  
    15  static double gettimedouble(void) {
    16      struct timeval tv;
    17      gettimeofday(&tv, NULL);
    18      return tv.tv_usec * 0.000001 + tv.tv_sec;
    19  }
    20  
    21  void print_number(double x) {
    22      double y = x;
    23      int c = 0;
    24      if (y < 0.0) {
    25          y = -y;
    26      }
    27      while (y > 0 && y < 100.0) {
    28          y *= 10.0;
    29          c++;
    30      }
    31      printf("%.*f", c, x);
    32  }
    33  
    34  void run_benchmark(char *name, void (*benchmark)(void*), void (*setup)(void*), void (*teardown)(void*), void* data, int count, int iter) {
    35      int i;
    36      double min = HUGE_VAL;
    37      double sum = 0.0;
    38      double max = 0.0;
    39      for (i = 0; i < count; i++) {
    40          double begin, total;
    41          if (setup != NULL) {
    42              setup(data);
    43          }
    44          begin = gettimedouble();
    45          benchmark(data);
    46          total = gettimedouble() - begin;
    47          if (teardown != NULL) {
    48              teardown(data);
    49          }
    50          if (total < min) {
    51              min = total;
    52          }
    53          if (total > max) {
    54              max = total;
    55          }
    56          sum += total;
    57      }
    58      printf("%s: min ", name);
    59      print_number(min * 1000000.0 / iter);
    60      printf("us / avg ");
    61      print_number((sum / count) * 1000000.0 / iter);
    62      printf("us / max ");
    63      print_number(max * 1000000.0 / iter);
    64      printf("us\n");
    65  }
    66  
    67  int have_flag(int argc, char** argv, char *flag) {
    68      char** argm = argv + argc;
    69      argv++;
    70      if (argv == argm) {
    71          return 1;
    72      }
    73      while (argv != NULL && argv != argm) {
    74          if (strcmp(*argv, flag) == 0) {
    75              return 1;
    76          }
    77          argv++;
    78      }
    79      return 0;
    80  }
    81  
    82  #endif /* SECP256K1_BENCH_H */