From e98fdfa9a160168d1de8c9a1a3ffebb3fdb4611b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Wed, 6 Oct 2010 21:19:23 +0200
Subject: [PATCH] (overhead): New global variable. (time_function): Compensate
 for call overhead. (bench_nothing, time_overhead): New functions.
 (time_memxor): Tweaked src size, making it an integral number of words.
 (main): Call time_overhead.

Rev: nettle/examples/nettle-benchmark.c:1.13
---
 examples/nettle-benchmark.c | 42 ++++++++++++++++++++++++++++++-------
 1 file changed, 34 insertions(+), 8 deletions(-)

diff --git a/examples/nettle-benchmark.c b/examples/nettle-benchmark.c
index 4e262629..2195b0e7 100644
--- a/examples/nettle-benchmark.c
+++ b/examples/nettle-benchmark.c
@@ -93,11 +93,15 @@ static double frequency = 0.0;
 #define BENCH_ITERATIONS 10
 #endif
 
+static double overhead = 0.0; 
+
 /* Returns second per function call */
 static double
 time_function(void (*f)(void *arg), void *arg)
 {
   unsigned ncalls;
+  double elapsed;
+
 #if HAVE_CLOCK_GETTIME && defined CLOCK_PROCESS_CPUTIME_ID
   struct timespec before;
   struct timespec after;
@@ -121,9 +125,10 @@ time_function(void (*f)(void *arg), void *arg)
     }
   while (after.tv_sec < done.tv_sec
 	 || (after.tv_nsec < done.tv_nsec && after.tv_sec == done.tv_sec));
-  
-  return (after.tv_sec - before.tv_sec
-	  + 1e-9 * (after.tv_nsec - before.tv_nsec)) / ncalls;
+
+  elapsed = after.tv_sec - before.tv_sec
+	     + 1e-9 * (after.tv_nsec - before.tv_nsec);
+
 #else /* !HAVE_CLOCK_GETTIME */
   clock_t before;
   clock_t after;
@@ -140,9 +145,16 @@ time_function(void (*f)(void *arg), void *arg)
       ncalls++;
     }
   while (after < done);
-  
-  return ((double)(after - before)) / CLOCKS_PER_SEC / ncalls;
+
+  elapsed = (double)(after - before) / CLOCKS_PER_SEC;
 #endif /* !HAVE_CLOCK_GETTIME */
+  return elapsed / ncalls - overhead;
+}
+
+static void
+bench_nothing(void *arg UNUSED)
+{
+  return;
 }
 
 struct bench_memxor_info
@@ -274,12 +286,24 @@ xalloc(size_t size)
   return p;
 }
 
+static void
+time_overhead(void)
+{
+  overhead = time_function(bench_nothing, NULL);
+  printf("benchmark call overhead: %7f us", overhead * 1e6);
+  if (frequency > 0.0)
+    printf("%7.2f cycles\n", overhead * frequency);
+  printf("\n");  
+}
+
+
+
 static void
 time_memxor(void)
 {
   struct bench_memxor_info info;
-  uint8_t src[BENCH_BLOCK + 1];
-  uint8_t dst[BENCH_BLOCK + 1];
+  uint8_t src[BENCH_BLOCK + sizeof(long)];
+  uint8_t dst[BENCH_BLOCK];
 
   info.src = src;
   info.dst = dst;
@@ -497,9 +521,11 @@ main(int argc, char **argv)
     }
 
   alg = argv[optind];
-  
+
   bench_sha1_compress();
 
+  time_overhead();
+
   header();
 
   if (!alg || strstr ("memxor", alg))
-- 
GitLab