Skip to content

Commit 60f3d4d

Browse files
committed
Pass num of iters to benchmarks as variable, and define macros
1 parent 02dd5f1 commit 60f3d4d

File tree

7 files changed

+139
-114
lines changed

7 files changed

+139
-114
lines changed

src/bench.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void print_number(const int64_t x) {
7373
printf("%s", &buffer[ptr]);
7474
}
7575

76-
void run_benchmark(char *name, void (*benchmark)(void*), void (*setup)(void*), void (*teardown)(void*), void* data, int count, int iter) {
76+
void run_benchmark(char *name, void (*benchmark)(void*, int), void (*setup)(void*), void (*teardown)(void*, int), void* data, int count, int iter) {
7777
int i;
7878
int64_t min = INT64_MAX;
7979
int64_t sum = 0;
@@ -84,10 +84,10 @@ void run_benchmark(char *name, void (*benchmark)(void*), void (*setup)(void*), v
8484
setup(data);
8585
}
8686
begin = gettime_i64();
87-
benchmark(data);
87+
benchmark(data, iter);
8888
total = gettime_i64() - begin;
8989
if (teardown != NULL) {
90-
teardown(data);
90+
teardown(data, iter);
9191
}
9292
if (total < min) {
9393
min = total;

src/bench_ecdh.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include "util.h"
1212
#include "bench.h"
1313

14+
#ifndef ITERS
15+
#define ITERS 20000
16+
#endif
17+
1418
typedef struct {
1519
secp256k1_context *ctx;
1620
secp256k1_pubkey point;
@@ -34,12 +38,12 @@ static void bench_ecdh_setup(void* arg) {
3438
CHECK(secp256k1_ec_pubkey_parse(data->ctx, &data->point, point, sizeof(point)) == 1);
3539
}
3640

37-
static void bench_ecdh(void* arg) {
41+
static void bench_ecdh(void* arg, int iters) {
3842
int i;
3943
unsigned char res[32];
4044
bench_ecdh_data *data = (bench_ecdh_data*)arg;
4145

42-
for (i = 0; i < 20000; i++) {
46+
for (i = 0; i < iters; i++) {
4347
CHECK(secp256k1_ecdh(data->ctx, res, &data->point, data->scalar, NULL, NULL) == 1);
4448
}
4549
}
@@ -50,7 +54,7 @@ int main(void) {
5054
/* create a context with no capabilities */
5155
data.ctx = secp256k1_context_create(SECP256K1_FLAGS_TYPE_CONTEXT);
5256

53-
run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, 20000);
57+
run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, ITERS);
5458

5559
secp256k1_context_destroy(data.ctx);
5660
return 0;

src/bench_ecmult.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include "secp256k1.c"
1919

2020
#define POINTS 32768
21+
#ifndef ITERS
2122
#define ITERS 10000
23+
#endif
2224

2325
typedef struct {
2426
/* Setup once in advance */
@@ -55,13 +57,13 @@ static int bench_callback(secp256k1_scalar* sc, secp256k1_ge* ge, size_t idx, vo
5557
return 1;
5658
}
5759

58-
static void bench_ecmult(void* arg) {
60+
static void bench_ecmult(void* arg, int iters) {
5961
bench_data* data = (bench_data*)arg;
6062

61-
size_t count = data->count;
6263
int includes_g = data->includes_g;
63-
size_t iters = 1 + ITERS / count;
64-
size_t iter;
64+
int iter;
65+
int count = data->count;
66+
iters = iters / data->count;
6567

6668
for (iter = 0; iter < iters; ++iter) {
6769
data->ecmult_multi(&data->ctx->error_callback, &data->ctx->ecmult_ctx, data->scratch, &data->output[iter], data->includes_g ? &data->scalars[data->offset1] : NULL, bench_callback, arg, count - includes_g);
@@ -76,10 +78,10 @@ static void bench_ecmult_setup(void* arg) {
7678
data->offset2 = (data->count * 0x7f6f537b + 0x6a1a8f49) % POINTS;
7779
}
7880

79-
static void bench_ecmult_teardown(void* arg) {
81+
static void bench_ecmult_teardown(void* arg, int iters) {
8082
bench_data* data = (bench_data*)arg;
81-
size_t iters = 1 + ITERS / data->count;
82-
size_t iter;
83+
int iter;
84+
iters = iters / data->count;
8385
/* Verify the results in teardown, to avoid doing comparisons while benchmarking. */
8486
for (iter = 0; iter < iters; ++iter) {
8587
secp256k1_gej tmp;
@@ -188,11 +190,14 @@ int main(int argc, char **argv) {
188190
run_test(&data, i, 1);
189191
}
190192

191-
for (p = 0; p <= 11; ++p) {
192-
for (i = 9; i <= 16; ++i) {
193-
run_test(&data, i << p, 1);
193+
if (ITERS > 1) {
194+
for (p = 0; p <= 11; ++p) {
195+
for (i = 9; i <= 16; ++i) {
196+
run_test(&data, i << p, 1);
197+
}
194198
}
195199
}
200+
196201
if (data.scratch != NULL) {
197202
secp256k1_scratch_space_destroy(data.ctx, data.scratch);
198203
}

0 commit comments

Comments
 (0)