diff --git a/bench/Makefile b/bench/Makefile index a3994367..037b2ab6 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -3,15 +3,29 @@ CXX=c++ -std=c++11 CFLAGS=-O2 -march=native -Wall CPPFLAGS=-I.. -all: bench-mlib bench-stl +all: bench-mlib bench-stl bench-klib bench-mlib: $(CC) $(CFLAGS) $(CPPFLAGS) bench-mlib.c -o bench-mlib.exe - ./bench-mlib.exe + @./bench-mlib.exe 1 + @./bench-mlib.exe 2 + @./bench-mlib.exe 3 + @./bench-mlib.exe 4 bench-stl: $(CXX) $(CFLAGS) $(CPPFLAGS) bench-stl.cpp -o bench-stl.exe - ./bench-stl.exe + @./bench-stl.exe 1 + @./bench-stl.exe 2 + @./bench-stl.exe 3 + @./bench-stl.exe 4 + @./bench-stl.exe 5 + +bench-klib: + $(CC) $(CFLAGS) $(CPPFLAGS) $${KLIB_CPPFLAGS} bench-klib.c -o bench-klib.exe + @./bench-klib.exe 1 + @./bench-klib.exe 2 + @./bench-klib.exe 3 + @./bench-klib.exe 4 clean: - rm -f *.o bench-mlib.exe bench-stl.exe *~ + rm -f *.o bench-*.exe *~ diff --git a/bench/bench-klib.c b/bench/bench-klib.c new file mode 100644 index 00000000..95516354 --- /dev/null +++ b/bench/bench-klib.c @@ -0,0 +1,164 @@ +#include +#include +#include +#include +#include +#include + +#define NDEBUG + +#include "kvec.h" +#include "klist.h" +#include "kbtree.h" +#include "khash.h" + +static unsigned long long +cputime (void) +{ + struct rusage rus; + getrusage (0, &rus); + return rus.ru_utime.tv_sec * 1000000ULL + rus.ru_utime.tv_usec; +} + +static unsigned int randValue = 0; +static void rand_init(void) +{ + randValue = 0; +} +static unsigned int rand_get(void) +{ + randValue = randValue * 31421U + 6927U; + return randValue; +} + +static void test_function(const char str[], size_t n, void (*func)(size_t)) +{ + unsigned long long start, end; + // (*func)(n); + start = cputime(); + (*func)(n); + end = cputime(); + end = (end - start) / 1000U; + printf ("%s %Lu ms for n = %lu\n", str, end, n); +} + +/********************************************************************************************/ + +static void test_array(size_t n) +{ + rand_init(); + kvec_t(unsigned int) a1, a2; + + kv_init(a1); + kv_init(a2); + + for(size_t i = 0; i < n; i++) { + kv_push(unsigned int, a1, rand_get() ); + kv_push(unsigned int, a2, rand_get() ); + } + unsigned int s = 0; + for(unsigned long i = 0; i < n; i++) { + s += kv_A(a1, i ) * kv_A(a2, i ); + } + + kv_destroy(a1); + kv_destroy(a2); +} + +/********************************************************************************************/ + +#define __int_free(x) +KLIST_INIT(32, unsigned int, __int_free) + +static void test_list (size_t n) +{ + rand_init(); + klist_t(32) *a1, *a2; + a1 = kl_init(32); + a2 = kl_init(32); + + for(size_t i = 0; i < n; i++) { + *kl_pushp (32, a1) = rand_get(); + *kl_pushp (32, a2) = rand_get(); + } + unsigned int s = 0; + kliter_t(32) *it1, *it2; + for(it1 = kl_begin(a1), it2 = kl_begin(a2); + it1 != kl_end(a1); + it1 = kl_next(it1), it2 = kl_next(it2)) { + s += kl_val(it1) * kl_val(it2); + } + + kl_destroy(32, a1); + kl_destroy(32, a2); +} + +/********************************************************************************************/ + +#define my_cmp(a,b) ( (a) < (b) ? -1 : (a) > (b) ) + +KBTREE_INIT(kTree, unsigned long, my_cmp) + +static void test_rbtree(size_t n) +{ + rand_init(); + kbtree_t(kTree) *tree; + tree = kb_init(kTree, KB_DEFAULT_SIZE/2); + + for (size_t i = 0; i < n; i++) { + unsigned long j = rand_get(); + kb_putp(kTree, tree, &j); + } + + unsigned int s = 0; + for (size_t i = 0; i < n; i++) { + unsigned long j = 0; + if (kb_getp(kTree, tree, &j) != 0) + s+= j; + } + kb_destroy(kTree, tree); +} + +/********************************************************************************************/ + +#define hash_eq(a, b) ((a) == (b)) +#define hash_func(a) ((a)) + +KHASH_INIT(iun, unsigned long, unsigned long, 1, hash_func, hash_eq) + +static void +test_dict(unsigned long n) +{ + rand_init(); + khash_t(iun) *dict = kh_init(iun); + + for (size_t i = 0; i < n; i++) { + int ret; + khiter_t k = kh_put(iun, dict, rand_get(), &ret); + kh_value(dict, k) = rand_get(); + } + unsigned int s = 0; + for (size_t i = 0; i < n; i++) { + khiter_t k = kh_get(iun, dict, rand_get()); + if (kh_exist(dict, k)) + s += kh_value(dict, k); + } + + kh_destroy(iun, dict); +} + +/********************************************************************************************/ + +int main(int argc, const char *argv[]) +{ + int n = (argc > 1) ? atoi(argv[1]): 0; + if (n == 1) + test_function("List time",10000000, test_list); + if (n == 2) + test_function("Array time", 100000000, test_array); + if (n == 3) + test_function("B-tree time", 1000000, test_rbtree); + if (n == 4) + test_function("Dict time", 1000000, test_dict); +} + diff --git a/bench/bench-mlib.c b/bench/bench-mlib.c index 3200e153..f9546f8c 100644 --- a/bench/bench-mlib.c +++ b/bench/bench-mlib.c @@ -28,7 +28,7 @@ static void rand_init(void) static unsigned int rand_get(void) { randValue = randValue * 31421U + 6927U; - return (int) randValue; + return randValue; } static void test_function(const char str[], size_t n, void (*func)(size_t)) @@ -128,9 +128,14 @@ test_dict(unsigned long n) int main(int argc, const char *argv[]) { - test_function("List time",10000000, test_list); - test_function("Array time", 100000000, test_array); - test_function("Rbtree time", 1000000, test_rbtree); - test_function("Dict time", 1000000, test_dict); + int n = (argc > 1) ? atoi(argv[1]) : 0; + if (n == 1) + test_function("List time",10000000, test_list); + if (n == 2) + test_function("Array time", 100000000, test_array); + if (n == 3) + test_function("Rbtree time", 1000000, test_rbtree); + if (n == 4) + test_function("Dict time", 1000000, test_dict); } diff --git a/bench/bench-stl.cpp b/bench/bench-stl.cpp index 4c79b87f..55587e20 100644 --- a/bench/bench-stl.cpp +++ b/bench/bench-stl.cpp @@ -32,7 +32,7 @@ static void rand_init(void) static unsigned int rand_get(void) { randValue = randValue * 31421U + 6927U; - return (int) randValue; + return randValue; } static void test_function(const char str[], size_t n, void (*func)(size_t)) @@ -141,9 +141,15 @@ test_dict2(unsigned long n) int main(int argc, const char *argv[]) { - test_function("List time",10000000, test_list); - test_function("Array time", 100000000, test_array); - test_function("Rbtree time", 1000000, test_rbtree); - test_function("Dict(m)time", 1000000, test_dict1); - test_function("Dict(u)time", 1000000, test_dict2); + int n = (argc > 1) ? atoi(argv[1]) : 0; + if (n == 1) + test_function("List time",10000000, test_list); + if (n == 2) + test_function("Array time", 100000000, test_array); + if (n == 3) + test_function("Rbtree time", 1000000, test_rbtree); + if (n == 4) + test_function("Dict(m)time", 1000000, test_dict1); + if (n == 5) + test_function("Dict(u)time", 1000000, test_dict2); }