Browse Source

mem: add memzero benchmark.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Rusty Russell 10 years ago
parent
commit
ca2551bced
3 changed files with 67 additions and 0 deletions
  1. 1 0
      ccan/mem/bench/.gitignore
  2. 17 0
      ccan/mem/bench/Makefile
  3. 49 0
      ccan/mem/bench/speed.c

+ 1 - 0
ccan/mem/bench/.gitignore

@@ -0,0 +1 @@
+speed

+ 17 - 0
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 $@ $<

+ 49 - 0
ccan/mem/bench/speed.c

@@ -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;
+}