From ca2551bcedb6ebd66814a2d4fdce7494b19d9b0d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 15 Oct 2015 14:18:47 +1030 Subject: [PATCH] mem: add memzero benchmark. Signed-off-by: Rusty Russell --- ccan/mem/bench/.gitignore | 1 + ccan/mem/bench/Makefile | 17 ++++++++++++++ ccan/mem/bench/speed.c | 49 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 ccan/mem/bench/.gitignore create mode 100644 ccan/mem/bench/Makefile create mode 100644 ccan/mem/bench/speed.c diff --git a/ccan/mem/bench/.gitignore b/ccan/mem/bench/.gitignore new file mode 100644 index 00000000..18426946 --- /dev/null +++ b/ccan/mem/bench/.gitignore @@ -0,0 +1 @@ +speed diff --git a/ccan/mem/bench/Makefile b/ccan/mem/bench/Makefile new file mode 100644 index 00000000..0f0dc775 --- /dev/null +++ b/ccan/mem/bench/Makefile @@ -0,0 +1,17 @@ +CCANDIR=../../.. +CFLAGS=-Wall -Werror -O3 -I$(CCANDIR) +#CFLAGS=-Wall -Werror -g -I$(CCANDIR) + +all: speed + +CCAN_OBJS:=ccan-mem.o ccan-time.o + +speed: speed.o $(CCAN_OBJS) + +clean: + rm -f speed *.o + +ccan-time.o: $(CCANDIR)/ccan/time/time.c + $(CC) $(CFLAGS) -c -o $@ $< +ccan-mem.o: $(CCANDIR)/ccan/mem/mem.c + $(CC) $(CFLAGS) -c -o $@ $< diff --git a/ccan/mem/bench/speed.c b/ccan/mem/bench/speed.c new file mode 100644 index 00000000..18938214 --- /dev/null +++ b/ccan/mem/bench/speed.c @@ -0,0 +1,49 @@ +/* Test speed of memiszero */ +#include +#include +#include +#include +#include + +#define MAX_TEST 65536 + +int main(int argc, char *argv[]) +{ + size_t n, i, max = argv[1] ? atol(argv[1]) : 100000000, runs; + char *arr; + size_t total = 0; + + arr = calloc(1, max + MAX_TEST + 1); + + runs = max; + /* First test even sizes case. */ + for (n = 1; n <= MAX_TEST; n *= 2) { + struct timeabs start = time_now(); + struct timerel each; + + for (i = 0; i < runs; i++) + total += memeqzero(arr + i, n); + each = time_divide(time_between(time_now(), start), runs); + assert(each.ts.tv_sec == 0); + printf("%zu: %uns\n", n, (unsigned int)each.ts.tv_nsec); + + /* Reduce runs over time, as bigger take longer. */ + runs = runs * 2 / 3; + } + + runs = max; + for (n = 1; n <= MAX_TEST; n *= 2) { + struct timeabs start = time_now(); + struct timerel each; + + for (i = 0; i < runs; i++) + total += memeqzero(arr + i, n+1); + each = time_divide(time_between(time_now(), start), runs); + assert(each.ts.tv_sec == 0); + printf("%zu: %uns\n", n+1, (unsigned int)each.ts.tv_nsec); + runs = runs * 2 / 3; + } + + printf("total = %zu\n", total); + return 0; +} -- 2.39.2