microbench: Add fallback to gettimeofday()
authorDave Yeo <dave.r.yeo@gmail.com>
Wed, 3 Feb 2016 04:19:59 +0000 (20:19 -0800)
committerErik de Castro Lopo <erikd@mega-nerd.com>
Tue, 9 Feb 2016 09:30:29 +0000 (20:30 +1100)
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>
configure.ac
microbench/util.c

index 915869b..b577248 100644 (file)
@@ -371,6 +371,11 @@ AC_DEFINE(FLAC__HAS_DOCBOOK_TO_MAN)
 AH_TEMPLATE(FLAC__HAS_DOCBOOK_TO_MAN, [define if you have docbook-to-man or docbook2man])
 fi
 
+AC_CHECK_LIB(rt, clock_gettime, have_clock_gettime=yes)
+if test x$have_clock_gettime = xyes; then
+        AC_DEFINE(HAVE_CLOCK_GETTIME)
+fi
+
 # only matters for x86
 AC_CHECK_PROGS(NASM, nasm)
 AM_CONDITIONAL(FLaC__HAS_NASM, test -n "$NASM")
index ff1c3fb..dbd4bc1 100644 (file)
@@ -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