From 02e6fb3c3422b8af26133638bca38c8a5a5fa3fb Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 25 Jun 2015 15:28:52 +0930 Subject: [PATCH] crypto/sha256: add benchmark. Signed-off-by: Rusty Russell --- ccan/crypto/sha256/benchmarks/Makefile | 10 ++++++++ .../sha256/benchmarks/double-sha-bench.c | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 ccan/crypto/sha256/benchmarks/Makefile create mode 100644 ccan/crypto/sha256/benchmarks/double-sha-bench.c diff --git a/ccan/crypto/sha256/benchmarks/Makefile b/ccan/crypto/sha256/benchmarks/Makefile new file mode 100644 index 00000000..9621962a --- /dev/null +++ b/ccan/crypto/sha256/benchmarks/Makefile @@ -0,0 +1,10 @@ +CCANDIR := ../../../../ +CFLAGS := -Wall -I$(CCANDIR) -O3 -flto +LDFLAGS := -O3 -flto + +double-sha-bench: double-sha-bench.o ccan-time.o ccan-crypto-sha256.o + +ccan-crypto-sha256.o: $(CCANDIR)/ccan/crypto/sha256/sha256.c + $(CC) $(CFLAGS) -c -o $@ $< +ccan-time.o: $(CCANDIR)/ccan/time/time.c + $(CC) $(CFLAGS) -c -o $@ $< diff --git a/ccan/crypto/sha256/benchmarks/double-sha-bench.c b/ccan/crypto/sha256/benchmarks/double-sha-bench.c new file mode 100644 index 00000000..93fd9d31 --- /dev/null +++ b/ccan/crypto/sha256/benchmarks/double-sha-bench.c @@ -0,0 +1,23 @@ +/* Bitcoin does a lot of SHA of SHA. Benchmark that. */ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + struct timeabs start; + struct timerel diff; + size_t i, n; + struct sha256 h; + + n = atoi(argv[1] ? argv[1] : "1000000"); + sha256(&h, &n, sizeof(n)); + start = time_now(); + for (i = 0; i < n; i++) + sha256(&h, &h, sizeof(h)); + diff = time_divide(time_between(time_now(), start), n); + printf("Hashing time for %02x%02x%02x%02x%02x%02x... is %llu nsec\n", + h.u.u8[0], h.u.u8[1], h.u.u8[2], h.u.u8[3], h.u.u8[4], h.u.u8[5], + (unsigned long long)time_to_nsec(diff)); + return 0; +} -- 2.39.2