Commit 040574d2 authored by Niels Möller's avatar Niels Möller
Browse files

(time_function): Loop around calling

f, until 1s has elapsed. Returns seconds per call. Updated bench
functions to not loop themselves.
(display): Updated MB/s calculation.

Rev: src/nettle/examples/nettle-benchmark.c:1.9
parent c284ea4a
...@@ -50,26 +50,35 @@ ...@@ -50,26 +50,35 @@
#include "cbc.h" #include "cbc.h"
/* Encrypt 10MB, 1K at a time. */ /* Encrypt 1K at a time, for one second */
#define BENCH_BLOCK 1024 #define BENCH_BLOCK 1024
#define BENCH_COUNT 10240 #define BENCH_INTERVAL CLOCKS_PER_SEC
/* Total MB:s, for MB/s figures. */ /* Total MB:s, for MB/s figures. */
#define BENCH_TOTAL 10.0 #define BENCH_TOTAL 10.0
/* Returns second per function call */
static double static double
time_function(void (*f)(void *arg), void *arg) time_function(void (*f)(void *arg), void *arg)
{ {
clock_t before; clock_t before;
clock_t after; clock_t after;
clock_t done;
unsigned ncalls;
before = clock(); before = clock();
done = before + BENCH_INTERVAL;
ncalls = 0;
f(arg); do
{
f(arg);
after = clock();
ncalls++;
}
while (after < done);
after = clock(); return ((double)(after - before)) / CLOCKS_PER_SEC / ncalls;
return ((double)(after - before)) / CLOCKS_PER_SEC;
} }
struct bench_cipher_info struct bench_cipher_info
...@@ -83,10 +92,7 @@ static void ...@@ -83,10 +92,7 @@ static void
bench_cipher(void *arg) bench_cipher(void *arg)
{ {
struct bench_cipher_info *info = arg; struct bench_cipher_info *info = arg;
unsigned i; info->crypt(info->ctx, BENCH_BLOCK, info->data, info->data);
for (i = 0; i<BENCH_COUNT; i++)
info->crypt(info->ctx, BENCH_BLOCK, info->data, info->data);
} }
struct bench_cbc_info struct bench_cbc_info
...@@ -104,24 +110,18 @@ static void ...@@ -104,24 +110,18 @@ static void
bench_cbc_encrypt(void *arg) bench_cbc_encrypt(void *arg)
{ {
struct bench_cbc_info *info = arg; struct bench_cbc_info *info = arg;
unsigned i; cbc_encrypt(info->ctx, info->crypt,
info->block_size, info->iv,
for (i = 0; i<BENCH_COUNT; i++) BENCH_BLOCK, info->data, info->data);
cbc_encrypt(info->ctx, info->crypt,
info->block_size, info->iv,
BENCH_BLOCK, info->data, info->data);
} }
static void static void
bench_cbc_decrypt(void *arg) bench_cbc_decrypt(void *arg)
{ {
struct bench_cbc_info *info = arg; struct bench_cbc_info *info = arg;
unsigned i; cbc_decrypt(info->ctx, info->crypt,
info->block_size, info->iv,
for (i = 0; i<BENCH_COUNT; i++) BENCH_BLOCK, info->data, info->data);
cbc_decrypt(info->ctx, info->crypt,
info->block_size, info->iv,
BENCH_BLOCK, info->data, info->data);
} }
/* Set data[i] = floor(sqrt(i)) */ /* Set data[i] = floor(sqrt(i)) */
...@@ -148,11 +148,11 @@ init_key(unsigned length, ...@@ -148,11 +148,11 @@ init_key(unsigned length,
static void static void
display(const char *name, const char *mode, display(const char *name, const char *mode,
double elapsed) double speed)
{ {
printf("%13s (%s): %.2fs, %.3fMB/s\n", printf("%13s (%s): %.3fMB/s\n",
name, mode, name, mode,
elapsed, BENCH_TOTAL / elapsed); 1 / (speed * 1048576.0 / BENCH_BLOCK));
} }
static void static void
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment