1 /*
2  * bench.c
3  * Copyright (C) 2014 c9s <c9s@c9smba.local>
4  *
5  * Distributed under terms of the MIT license.
6  */
7 
8 #include "bench.h"
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <sys/time.h>
12 
unixtime()13 long unixtime() {
14     struct timeval tp;
15     long sec = 0L;
16     if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) {
17         return tp.tv_sec;
18     }
19     return 0;
20 }
21 
microtime()22 double microtime() {
23     struct timeval tp;
24     long sec = 0L;
25     double msec = 0.0;
26     char ret[100];
27 
28     if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) {
29         msec = (double) (tp.tv_usec / MICRO_IN_SEC);
30         sec = tp.tv_sec;
31         if (msec >= 1.0)
32             msec -= (long) msec;
33         return sec + msec;
34     }
35     return 0;
36 }
37 
38 
bench_start(bench * b)39 void bench_start(bench *b) {
40     b->start = microtime();
41 }
42 
bench_stop(bench * b)43 void bench_stop(bench *b) {
44     b->end = microtime();
45 }
46 
bench_iteration_speed(bench * b)47 double bench_iteration_speed(bench *b) {
48     return b->N / (b->end - b->start);
49 }
50 
bench_print_summary(bench * b)51 void bench_print_summary(bench *b) {
52     printf("%ld runs, ", b->R);
53     printf("%ld iterations each run, ", b->N);
54     printf("finished in %lf seconds\n", b->end - b->start );
55     printf("%.2f i/sec\n", (b->N * b->R) / (b->end - b->start) );
56 }
57