commit c018cebc38c37370ab6c89d19694587edf7d0d0e
Author:     Mattias Andrée <[email protected]>
AuthorDate: Wed Mar 9 19:12:57 2016 +0100
Commit:     Mattias Andrée <[email protected]>
CommitDate: Wed Mar 9 19:12:57 2016 +0100

    Add simple benchmarker
    
    Signed-off-by: Mattias Andrée <[email protected]>

diff --git a/.gitignore b/.gitignore
index 68ad537..2c97130 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@
 *.so
 /test
 /test-random.c
+/benchmark
diff --git a/Makefile b/Makefile
index 2e8d2d5..5a42f89 100644
--- a/Makefile
+++ b/Makefile
@@ -82,6 +82,9 @@ test-random.c: test-generate.py
 test: test.c libzahl.a test-random.c
        $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ test.c libzahl.a
 
+benchmark: bench/benchmark.c libzahl.a
+       $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ $^
+
 check: test
        ./test
 
diff --git a/bench/benchmark.c b/bench/benchmark.c
new file mode 100644
index 0000000..7d062a6
--- /dev/null
+++ b/bench/benchmark.c
@@ -0,0 +1,132 @@
+#include <time.h>
+#include <stdio.h>
+
+#include "../zahl.h"
+
+
+#ifndef CLOCK_MONOTONIC_RAW
+# define CLOCK_MONOTONIC_RAW  CLOCK_MONOTONIC
+#endif
+
+
+#define BENCHMARK(INSTRUCTION, FAST)\
+       do {\
+               i = FAST ? 1000000L : 1000L;\
+               clock_gettime(CLOCK_MONOTONIC_RAW, &start);\
+               while (i--) {\
+                       INSTRUCTION;\
+               }\
+               clock_gettime(CLOCK_MONOTONIC_RAW, &end);\
+               end.tv_sec -= start.tv_sec;\
+               end.tv_nsec -= start.tv_nsec;\
+               if (end.tv_nsec < 0) {\
+                       end.tv_nsec += 1000000000L;\
+                       end.tv_sec -= 1;\
+               }\
+               printf("%s: %lli.%09li %s (152 bits)\n",\
+                      #INSTRUCTION,\
+                      (long long int)(end.tv_sec), end.tv_nsec,\
+                      FAST ? "µs" : "ms");\
+       } while (0)
+
+
+int
+main(int argc, char *argv[])
+{
+       struct timespec start, end;
+       char buf[1000];
+       z_t a, b, c, d, tiny;
+       jmp_buf jmp;
+       size_t i;
+
+       if (setjmp(jmp)) {
+               zperror(argv[0]);
+               return 1;
+       }
+       zsetup(jmp);
+       zinit(a);
+       zinit(b);
+       zinit(c);
+       zinit(d);
+       zinit(tiny);
+
+       zsets(a, "5495468234592964023447280368442884381000481887");
+       zsets(b, "4781084818570683458641843084358135840548636081");
+       zsets(tiny, "5");
+
+       BENCHMARK(zset(c, a), 1);
+       BENCHMARK(zseti(c, 1000000000LL), 1);
+       BENCHMARK(zsetu(c, 1000000000ULL), 1);
+       BENCHMARK(zneg(c, a), 1);
+       BENCHMARK(zneg(a, a), 1);
+       BENCHMARK(zabs(c, a), 1);
+       BENCHMARK(zabs(a, a), 1);
+       BENCHMARK(zadd_unsigned(c, a, b), 1);
+       BENCHMARK(zsub_unsigned(c, a, b), 1);
+       BENCHMARK(zadd(c, a, b), 1);
+       BENCHMARK(zsub(c, a, b), 1);
+       BENCHMARK(zand(c, a, b), 1);
+       BENCHMARK(zor(c, a, b), 1);
+       BENCHMARK(zxor(c, a, b), 1);
+       BENCHMARK(znot(c, a), 1);
+       BENCHMARK(zeven(a), 1);
+       BENCHMARK(zodd(a), 1);
+       BENCHMARK(zeven_nonzero(a), 1);
+       BENCHMARK(zodd_nonzero(a), 1);
+       BENCHMARK(zzero(a), 1);
+       BENCHMARK(zsignum(a), 1);
+       BENCHMARK(zbits(a), 1);
+       BENCHMARK(zlsb(a), 1);
+       BENCHMARK(zswap(a, b), 1);
+       BENCHMARK(zlsh(c, a, 76), 1);
+       BENCHMARK(zrsh(c, a, 76), 1);
+       BENCHMARK(ztrunc(c, a, 76), 1);
+       BENCHMARK(zsplit(c, d, a, 76), 1);
+       BENCHMARK(zcmpmag(a, b), 1);
+       BENCHMARK(zcmp(a, b), 1);
+       BENCHMARK(zcmpi(a, 1000000000LL), 1);
+       BENCHMARK(zcmpu(a, 1000000000ULL), 1);
+       BENCHMARK(zbset(c, a, 76, 1), 1);
+       BENCHMARK(zbset(c, a, 76, 0), 1);
+       BENCHMARK(zbset(c, a, 76, -1), 1);
+       BENCHMARK(zbtest(a, 76), 1);
+       BENCHMARK(zgcd(c, a, b), 0);
+       BENCHMARK(zmul(c, a, b), 0);
+       BENCHMARK(zmul(c, a, a), 0);
+       BENCHMARK(zsqr(c, a), 0);
+       zsets(d, "1484298084218938358480511181388394862858002249");
+       BENCHMARK(zmodmul(c, a, b, d), 0);
+       BENCHMARK(zmodmul(c, a, a, d), 0);
+       BENCHMARK(zmodsqr(c, a, d), 0);
+       BENCHMARK(zmodmul(c, a, b, tiny), 0);
+       BENCHMARK(zmodmul(c, a, a, tiny), 0);
+       BENCHMARK(zmodsqr(c, a, tiny), 0);
+       zsets(d, "12");
+       BENCHMARK(zpow(c, a, d), 0);
+       BENCHMARK(zpowu(c, a, 12), 0);
+       BENCHMARK(zmodpow(c, a, d, b), 0);
+       BENCHMARK(zmodpowu(c, a, 12, b), 0);
+       BENCHMARK(zsets(c, "5495468234592964023447280368442884381000481887"), 
0);
+       BENCHMARK(zstr_length(a, 10), 0);
+       BENCHMARK(zstr(a, buf), 0);
+       BENCHMARK(zrand(c, FAST_RANDOM, QUASIUNIFORM, a), 0);
+       BENCHMARK(zrand(c, FAST_RANDOM, UNIFORM, a), 0);
+       BENCHMARK(zptest(d, a, 5), 0);
+       BENCHMARK(zsave(a, buf), 1);
+       BENCHMARK(zload(a, buf), 1);
+       BENCHMARK(zdiv(c, a, b), 1);
+       BENCHMARK(zmod(c, a, b), 1);
+       BENCHMARK(zdivmod(c, d, a, b), 1);
+       BENCHMARK(zdiv(c, a, tiny), 0);
+       BENCHMARK(zmod(c, a, tiny), 0);
+       BENCHMARK(zdivmod(c, d, a, tiny), 0);
+
+       zfree(a);
+       zfree(b);
+       zfree(c);
+       zfree(d);
+       zfree(tiny);
+       zunsetup();
+       return 0;
+       (void) argc;
+}

Reply via email to