summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorNiels Möller <nisse@lysator.liu.se>2010-09-22 14:57:47 +0200
committerNiels Möller <nisse@lysator.liu.se>2010-09-22 14:57:47 +0200
commit54ba21a9cf5934ad3c76c33c5bc461789a7fb6fc (patch)
tree9c20b6743ca425042ef8dd3721829aeb5095c2a9 /examples
parent1578b08990ef4aeae77414521745fbe8d658f4d5 (diff)
downloadnettle-54ba21a9cf5934ad3c76c33c5bc461789a7fb6fc.tar.gz
(BENCH_INTERVAL): Changed unit to
seconds. (time_function): Use clock_gettime with CLOCK_PROCESS_CPUTIME_ID, if available. This gives better accuracy, at least on recent linux. Rev: nettle/examples/nettle-benchmark.c:1.9
Diffstat (limited to 'examples')
-rw-r--r--examples/nettle-benchmark.c39
1 files changed, 32 insertions, 7 deletions
diff --git a/examples/nettle-benchmark.c b/examples/nettle-benchmark.c
index 95296ba5..9a1529d2 100644
--- a/examples/nettle-benchmark.c
+++ b/examples/nettle-benchmark.c
@@ -54,12 +54,9 @@
static double frequency = 0.0;
-/* Process BENCH_BLOCK bytes at a time, for BENCH_INTERVAL clocks. */
+/* Process BENCH_BLOCK bytes at a time, for BENCH_INTERVAL seconds. */
#define BENCH_BLOCK 10240
-#define BENCH_INTERVAL (CLOCKS_PER_SEC / 4)
-
-/* Total MB:s, for MB/s figures. */
-#define BENCH_TOTAL 10.0
+#define BENCH_INTERVAL 0.25
/* FIXME: Proper configure test for rdtsc? */
#ifndef WITH_CYCLE_COUNTER
@@ -87,13 +84,40 @@ static double frequency = 0.0;
static double
time_function(void (*f)(void *arg), void *arg)
{
+ unsigned ncalls;
+#if HAVE_CLOCK_GETTIME && defined CLOCK_PROCESS_CPUTIME_ID
+ struct timespec before;
+ struct timespec after;
+ struct timespec done;
+
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &before);
+ done = before;
+ done.tv_nsec += BENCH_INTERVAL * 1e9;
+ if (done.tv_nsec >= 1000000000L)
+ {
+ done.tv_nsec -= 1000000000L;
+ done.tv_sec ++;
+ }
+ ncalls = 0;
+
+ do
+ {
+ f(arg);
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &after);
+ ncalls++;
+ }
+ 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;
+#else /* !HAVE_CLOCK_GETTIME */
clock_t before;
clock_t after;
clock_t done;
- unsigned ncalls;
before = clock();
- done = before + BENCH_INTERVAL;
+ done = before + BENCH_INTERVAL * CLOCKS_PER_SEC;
ncalls = 0;
do
@@ -105,6 +129,7 @@ time_function(void (*f)(void *arg), void *arg)
while (after < done);
return ((double)(after - before)) / CLOCKS_PER_SEC / ncalls;
+#endif /* !HAVE_CLOCK_GETTIME */
}
struct bench_hash_info