From 2c83f6f444d7590f32947865520fc36601dade38 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 22 Jul 2017 13:11:29 +0200 Subject: [PATCH] Add bench for Tommy DS --- bench/Makefile | 9 +++++ bench/bench-tommyds.c | 92 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 bench/bench-tommyds.c diff --git a/bench/Makefile b/bench/Makefile index 70808812..994a2083 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -111,6 +111,15 @@ bench-collectionc0: @./bench-collectionc.exe 41 @./bench-collectionc.exe 50 +# TOMMYDS shall point to the source directory. +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 + @./bench-tommyds.exe 10 + @./bench-tommyds.exe 20 + bench-plain: $(CC) $(CFLAGS) $(CPPFLAGS) bench-plain-c.c -o bench-plain-c.exe @./bench-plain-c.exe diff --git a/bench/bench-tommyds.c b/bench/bench-tommyds.c new file mode 100644 index 00000000..30cc26d1 --- /dev/null +++ b/bench/bench-tommyds.c @@ -0,0 +1,92 @@ +#define NDEBUG + +#include +#include +#include +#include +#include +#include +#include + +#include "tommyds/tommy.h" + +#include "common.h" + +/********************************************************************************************/ + +static void test_array(size_t n) +{ + tommy_array a1, a2; + + tommy_array_init(&a1); + tommy_array_init(&a2); + for(size_t i = 0; i < n; i++) { + unsigned int v = rand_get(); + tommy_array_insert(&a1, (void*)(uintptr_t) v); + v = rand_get(); + tommy_array_insert(&a2, (void*)(uintptr_t) v); + } + unsigned int s = 0; + for(unsigned long i = 0; i < n; i++) { + s += (uintptr_t) *tommy_array_ref(&a1, i) * (uintptr_t) *tommy_array_ref(&a2, i ); + } + g_result = s; + tommy_array_done(&a1); + tommy_array_done(&a2); +} + +/********************************************************************************************/ + +struct list_node_s { + unsigned int value; + tommy_node node; +}; + +static void test_list (size_t n) +{ + tommy_list a1, a2; + + tommy_list_init(&a1); + tommy_list_init(&a2); + for(size_t i = 0; i < n; i++) { + struct list_node_s *obj; + obj = malloc(sizeof(struct list_node_s)); + if (obj == NULL) abort(); + obj->value = rand_get(); + tommy_list_insert_tail(&a1, &obj->node, obj); + obj = malloc(sizeof(struct list_node_s)); + if (obj == NULL) abort(); + obj->value = rand_get(); + tommy_list_insert_tail(&a2, &obj->node, obj); + } + unsigned int s = 0; + for(tommy_node *it1 = tommy_list_head(&a1), *it2 = tommy_list_head(&a2) + ; it1 != NULL; it1 = it1->next, it2 = it2->next ) { + struct list_node_s *obj1 = it1->data, *obj2 = it2->data; + s += obj1->value * obj2->value; + n--; + } + g_result = s; + tommy_list_foreach(&a1, free); + tommy_list_foreach(&a2, free); +} + +/********************************************************************************************/ + +int main(int argc, const char *argv[]) +{ + int n = (argc > 1) ? atoi(argv[1]) : 0; + if (n == 10) + test_function("List time",10000000, test_list); + if (n == 20) + test_function("Array time", 100000000, test_array); + /* if (n == 30) + test_function("Rbtree time", 1000000, test_rbtree); + if (n == 40) + test_function("Dict time", 1000000, test_dict); + if (n == 41) + test_function("DictB time", 1000000, test_dict_big); + if (n == 50) + test_function("Sort time", 10000000, test_sort);*/ + exit(0); +}