diff options
Diffstat (limited to 'src/bench.h')
-rw-r--r-- | src/bench.h | 63 |
1 files changed, 51 insertions, 12 deletions
diff --git a/src/bench.h b/src/bench.h index 63c55df44d..aa275fe919 100644 --- a/src/bench.h +++ b/src/bench.h @@ -24,7 +24,7 @@ static int64_t gettime_i64(void) { /* Format fixed point number. */ void print_number(const int64_t x) { int64_t x_abs, y; - int c, i, rounding; + int c, i, rounding, g; /* g = integer part size, c = fractional part size */ size_t ptr; char buffer[30]; @@ -56,21 +56,27 @@ void print_number(const int64_t x) { /* Format and print the number. */ ptr = sizeof(buffer) - 1; buffer[ptr] = 0; - if (c != 0) { + g = 0; + if (c != 0) { /* non zero fractional part */ for (i = 0; i < c; ++i) { buffer[--ptr] = '0' + (y % 10); y /= 10; } - buffer[--ptr] = '.'; + } else if (c == 0) { /* fractional part is 0 */ + buffer[--ptr] = '0'; } + buffer[--ptr] = '.'; do { buffer[--ptr] = '0' + (y % 10); y /= 10; + g++; } while (y != 0); if (x < 0) { buffer[--ptr] = '-'; + g++; } - printf("%s", &buffer[ptr]); + printf("%5.*s", g, &buffer[ptr]); /* Prints integer part */ + printf("%-*s", FP_EXP, &buffer[ptr + g]); /* Prints fractional part */ } void run_benchmark(char *name, void (*benchmark)(void*, int), void (*setup)(void*), void (*teardown)(void*, int), void* data, int count, int iter) { @@ -97,22 +103,20 @@ void run_benchmark(char *name, void (*benchmark)(void*, int), void (*setup)(void } sum += total; } - printf("%s: min ", name); + /* ',' is used as a column delimiter */ + printf("%-30s, ", name); print_number(min * FP_MULT / iter); - printf("us / avg "); + printf(" , "); print_number(((sum * FP_MULT) / count) / iter); - printf("us / max "); + printf(" , "); print_number(max * FP_MULT / iter); - printf("us\n"); + printf("\n"); } int have_flag(int argc, char** argv, char *flag) { char** argm = argv + argc; argv++; - if (argv == argm) { - return 1; - } - while (argv != NULL && argv != argm) { + while (argv != argm) { if (strcmp(*argv, flag) == 0) { return 1; } @@ -121,6 +125,32 @@ int have_flag(int argc, char** argv, char *flag) { return 0; } +/* takes an array containing the arguments that the user is allowed to enter on the command-line + returns: + - 1 if the user entered an invalid argument + - 0 if all the user entered arguments are valid */ +int have_invalid_args(int argc, char** argv, char** valid_args, size_t n) { + size_t i; + int found_valid; + char** argm = argv + argc; + argv++; + + while (argv != argm) { + found_valid = 0; + for (i = 0; i < n; i++) { + if (strcmp(*argv, valid_args[i]) == 0) { + found_valid = 1; /* user entered a valid arg from the list */ + break; + } + } + if (found_valid == 0) { + return 1; /* invalid arg found */ + } + argv++; + } + return 0; +} + int get_iters(int default_iters) { char* env = getenv("SECP256K1_BENCH_ITERS"); if (env) { @@ -130,4 +160,13 @@ int get_iters(int default_iters) { } } +void print_output_table_header_row(void) { + char* bench_str = "Benchmark"; /* left justified */ + char* min_str = " Min(us) "; /* center alignment */ + char* avg_str = " Avg(us) "; + char* max_str = " Max(us) "; + printf("%-30s,%-15s,%-15s,%-15s\n", bench_str, min_str, avg_str, max_str); + printf("\n"); +} + #endif /* SECP256K1_BENCH_H */ |