gitee.com/liu-zhao234568/cntest@v1.0.0/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 <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 < 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