From 05bdf16d1c5a18c7ab9f572469190e64f099a0f7 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 22 Jul 2017 13:22:48 +0200 Subject: [PATCH] Add tree benchmark for tommyds --- bench/Makefile | 3 ++- bench/bench-tommyds.c | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/bench/Makefile b/bench/Makefile index 994a2083..430eac65 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -116,9 +116,10 @@ bench-tommyds: if test -n "$${TOMMYDS}" ; then make bench-tommyds0 ; else echo "Nothing to be done." ; fi bench-tommyds0: - $(CC) $(CFLAGS) $(CPPFLAGS) -I "$${TOMMYDS}" bench-tommyds.c $${TOMMYDS}/tommyds/tommyarray.c $${TOMMYDS}/tommyds/tommylist.c -o bench-tommyds.exe + $(CC) $(CFLAGS) $(CPPFLAGS) -I "$${TOMMYDS}" bench-tommyds.c $${TOMMYDS}/tommyds/tommyarray.c $${TOMMYDS}/tommyds/tommylist.c $${TOMMYDS}/tommyds/tommytree.c -o bench-tommyds.exe @./bench-tommyds.exe 10 @./bench-tommyds.exe 20 + @./bench-tommyds.exe 30 bench-plain: $(CC) $(CFLAGS) $(CPPFLAGS) bench-plain-c.c -o bench-plain-c.exe diff --git a/bench/bench-tommyds.c b/bench/bench-tommyds.c index 30cc26d1..41a29316 100644 --- a/bench/bench-tommyds.c +++ b/bench/bench-tommyds.c @@ -73,6 +73,42 @@ static void test_list (size_t n) /********************************************************************************************/ +struct tree_node_s { + unsigned long value; + tommy_tree_node node; +} ; + +static int compare(const void *a, const void *b) +{ + const struct tree_node_s *pa = a; + const struct tree_node_s *pb = b; + return (pa->value < pb->value) ? -1 : (pa->value > pb->value); +} + +static void test_rbtree(size_t n) +{ + tommy_tree tree; + tommy_tree_init(&tree, compare); + for (size_t i = 0; i < n; i++) { + struct tree_node_s *obj = malloc(sizeof(struct tree_node_s)); + if (obj == NULL) abort(); + obj->value = rand_get(); + tommy_tree_insert(&tree, &obj->node, obj); + } + rand_init(); + unsigned int s = 0; + for (size_t i = 0; i < n; i++) { + struct tree_node_s key = { .value = rand_get() }; + struct tree_node_s *obj = tommy_tree_search (&tree, &key); + if (obj) + s += obj->value; + } + g_result = s; + tommy_tree_foreach(&tree, free); +} + +/********************************************************************************************/ + int main(int argc, const char *argv[]) { int n = (argc > 1) ? atoi(argv[1]) : 0; @@ -80,9 +116,9 @@ int main(int argc, const char *argv[]) test_function("List time",10000000, test_list); if (n == 20) test_function("Array time", 100000000, test_array); - /* if (n == 30) + if (n == 30) test_function("Rbtree time", 1000000, test_rbtree); - if (n == 40) + /*if (n == 40) test_function("Dict time", 1000000, test_dict); if (n == 41) test_function("DictB time", 1000000, test_dict_big);