]> git.ozlabs.org Git - ccan/commitdiff
mem: add memzero benchmark.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 15 Oct 2015 03:48:47 +0000 (14:18 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 15 Oct 2015 05:14:18 +0000 (15:44 +1030)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/mem/bench/.gitignore [new file with mode: 0644]
ccan/mem/bench/Makefile [new file with mode: 0644]
ccan/mem/bench/speed.c [new file with mode: 0644]

diff --git a/ccan/mem/bench/.gitignore b/ccan/mem/bench/.gitignore
new file mode 100644 (file)
index 0000000..1842694
--- /dev/null
@@ -0,0 +1 @@
+speed
diff --git a/ccan/mem/bench/Makefile b/ccan/mem/bench/Makefile
new file mode 100644 (file)
index 0000000..0f0dc77
--- /dev/null
@@ -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 (file)
index 0000000..1893821
--- /dev/null
@@ -0,0 +1,49 @@
+/* Test speed of memiszero */
+#include <ccan/time/time.h>
+#include <ccan/mem/mem.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+
+#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;
+}