summaryrefslogtreecommitdiff
path: root/microbench
diff options
context:
space:
mode:
authorDave Yeo <dave.r.yeo@gmail.com>2016-02-02 20:19:59 -0800
committerErik de Castro Lopo <erikd@mega-nerd.com>2016-02-09 20:30:29 +1100
commit50e7aea808ce113b23b0a74c62962c2fa5ac0b31 (patch)
tree8f852f8a5808ef628577e97cd8e1db1a25c941b7 /microbench
parentab61102209e1debc2818660f8e0be35541eb19b8 (diff)
downloadflac-50e7aea808ce113b23b0a74c62962c2fa5ac0b31.tar.gz
microbench: Add fallback to gettimeofday()
Some operating systems such as OS/2 don't have any of the CLOCK* API functions so add gettimeofday() as a fallback. Signed-off-by: Dave Yeo <dave.r.yeo@gmail.com> Signed-off-by: Erik de Castro Lopo <erikd@mega-nerd.com>
Diffstat (limited to 'microbench')
-rw-r--r--microbench/util.c38
1 files changed, 37 insertions, 1 deletions
diff --git a/microbench/util.c b/microbench/util.c
index ff1c3fb3..dbd4bc19 100644
--- a/microbench/util.c
+++ b/microbench/util.c
@@ -95,7 +95,7 @@ benchmark_function (void (*testfunc) (void), unsigned count)
return counter_diff (&start, &end) / count ;
} /* benchmark_function */
-#else
+#elif defined HAVE_CLOCK_GETTIME
#include <time.h>
#include <sys/time.h>
@@ -131,6 +131,42 @@ benchmark_function (void (*testfunc) (void), unsigned count)
return timespec_diff (&start, &end) / count ;
} /* benchmark_function */
+#else
+
+#include <time.h>
+#include <sys/time.h>
+
+static double
+timeval_diff (const struct timeval * start, const struct timeval * end)
+{ struct timeval diff;
+
+ if (end->tv_usec - start->tv_usec < 0)
+ { diff.tv_sec = end->tv_sec - start->tv_sec - 1 ;
+ diff.tv_usec = 1000000 + end->tv_usec - start->tv_usec ;
+ }
+ else
+ { diff.tv_sec = end->tv_sec - start->tv_sec ;
+ diff.tv_usec = end->tv_usec-start->tv_usec ;
+ } ;
+
+ return diff.tv_sec + 1e-6 * diff.tv_usec ;
+}
+
+double
+benchmark_function (void (*testfunc) (void), unsigned count)
+{ struct timeval start, end;
+ unsigned k ;
+
+ gettimeofday(&start, NULL);
+
+ for (k = 0 ; k < count ; k++)
+ testfunc () ;
+
+ gettimeofday(&end, NULL);
+
+ return timeval_diff (&start, &end) / count ;
+} /* benchmark_function */
+
#endif
static int