r3/tests/bench.c

61 lines
1.2 KiB
C
Raw Normal View History

2014-05-16 19:48:22 -04:00
/*
* bench.c
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#include "bench.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
long unixtime() {
struct timeval tp;
long sec = 0L;
if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) {
return tp.tv_sec;
}
return 0;
}
double microtime() {
struct timeval tp;
long sec = 0L;
double msec = 0.0;
char ret[100];
if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) {
msec = (double) (tp.tv_usec / MICRO_IN_SEC);
sec = tp.tv_sec;
if (msec >= 1.0)
msec -= (long) msec;
return sec + msec;
}
return 0;
}
void bench_start(bench *b) {
b->start = microtime();
}
void bench_stop(bench *b) {
b->end = microtime();
}
double bench_iteration_speed(bench *b) {
return (b->N * b->R) / (b->end - b->start);
}
double bench_duration(bench *b) {
return (b->end - b->start);
2014-05-16 19:48:22 -04:00
}
2014-05-16 19:48:57 -04:00
void bench_print_summary(bench *b) {
2014-05-16 19:48:22 -04:00
printf("%ld runs, ", b->R);
2014-05-16 19:52:03 -04:00
printf("%ld iterations each run, ", b->N);
printf("finished in %lf seconds\n", bench_duration(b) );
printf("%.2f i/sec\n", bench_iteration_speed(b) );
2014-05-16 19:48:22 -04:00
}