diff options
author | David Robillard <d@drobilla.net> | 2020-08-13 17:25:50 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2020-08-13 17:25:50 +0200 |
commit | adae13963a67d85d39ca990b262688a729a98edc (patch) | |
tree | 640b2030c5579db042cb46f47225f82cae6686bf /benchmark/bench.h | |
parent | 8d6de7dac62b0cef196cc1aaf5451a7ccf4d93fe (diff) | |
download | zix-adae13963a67d85d39ca990b262688a729a98edc.tar.gz zix-adae13963a67d85d39ca990b262688a729a98edc.tar.bz2 zix-adae13963a67d85d39ca990b262688a729a98edc.zip |
Move benchmarks to a separate directory
Diffstat (limited to 'benchmark/bench.h')
-rw-r--r-- | benchmark/bench.h | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/benchmark/bench.h b/benchmark/bench.h new file mode 100644 index 0000000..716601b --- /dev/null +++ b/benchmark/bench.h @@ -0,0 +1,44 @@ +/* + Copyright 2011 David Robillard <http://drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#define _POSIX_C_SOURCE 199309L + +#include <sys/time.h> +#include <time.h> + +static inline double +elapsed_s(const struct timespec* start, const struct timespec* end) +{ + return ( (double)(end->tv_sec - start->tv_sec) + + ((double)(end->tv_nsec - start->tv_nsec) * 0.000000001) ); +} + +static inline struct timespec +bench_start(void) +{ + struct timespec start_t; + clock_gettime(CLOCK_MONOTONIC, &start_t); + return start_t; +} + +static inline double +bench_end(const struct timespec* start_t) +{ + struct timespec end_t; + clock_gettime(CLOCK_MONOTONIC, &end_t); + return elapsed_s(start_t, &end_t); +} + |