From 84f73ab62379b45531a133c90ed313072b9b9bd2 Mon Sep 17 00:00:00 2001 From: Leonardo Vencovsky Date: Mon, 23 Jan 2023 10:08:14 +0100 Subject: [PATCH] Fixing benchmark --- Makefile | 3 +- benchmarks/Makefile | 7 ++ benchmarks/benchmark.c | 186 ++++++++++++++++------------ src/cmc/cmc/hashmultiset/code.h | 11 -- src/cmc/cmc/hashmultiset/ext/code.h | 13 ++ 5 files changed, 129 insertions(+), 91 deletions(-) create mode 100644 benchmarks/Makefile diff --git a/Makefile b/Makefile index 78c10d2..e186b52 100644 --- a/Makefile +++ b/Makefile @@ -15,5 +15,4 @@ format: - find ./ -name *.h -type f | xargs clang-format --style=file --verbose -i bench: - gcc ./benchmarks/benchmark.c -o ./benchmarks/benchmark.exe -I ./src -O3 - ./benchmarks/benchmark.exe + make -C ./benchmarks benchmark diff --git a/benchmarks/Makefile b/benchmarks/Makefile new file mode 100644 index 0000000..f7695a3 --- /dev/null +++ b/benchmarks/Makefile @@ -0,0 +1,7 @@ +CC=gcc +INCLUDE=-I ../src +CFLAGS=-Wall -Wextra -Werror + +benchmark: benchmark.c + $(CC) $(CFLAGS) $(INCLUDE) $< -o ./$@.exe -lm + ./$@.exe diff --git a/benchmarks/benchmark.c b/benchmarks/benchmark.c index e2884c4..9fc9a57 100644 --- a/benchmarks/benchmark.c +++ b/benchmarks/benchmark.c @@ -20,6 +20,8 @@ #include "util/twister.c" +#include "cmc/cor/hashtable.h" +#include "cmc/cor/heap.h" #include "cmc/utl/foreach.h" #include "cmc/utl/futils.h" #include "cmc/utl/timer.h" @@ -30,23 +32,6 @@ // Percentage % macro #define PC(x) x * 100.0 -static int intcmp(int a, int b) -{ - return a - b; -} - -static size_t inthash(int t) -{ - size_t a = t; - a += ~(a << 15); - a ^= (a >> 10); - a += (a << 3); - a ^= (a >> 6); - a += ~(a << 11); - a ^= (a >> 16); - return a; -} - size_t randsize(size_t min, size_t max, mt_state_ptr st) { return (size_t)floor(twist_real(st) * ((double)max - (double)min + 1.0) + (double)min); @@ -63,38 +48,82 @@ void shuffle(int *array, mt_state_ptr st) } } -#define K int32_t -#define V int32_t -#define PFX d +struct +{ + CMC_DEF_FTAB_CMP(size_t); + CMC_DEF_FTAB_CPY(size_t); + CMC_DEF_FTAB_STR(size_t); + CMC_DEF_FTAB_FREE(size_t); + CMC_DEF_FTAB_HASH(size_t); + CMC_DEF_FTAB_PRI(size_t); +} int_ftab = { cmc_size_cmp, NULL, cmc_size_str, NULL, cmc_size_hash, cmc_size_cmp }; + +#define CMC_ARGS_FALLTHROUGH +#define V int +#define K int +#define CMC_EXT_ITER + +#define SNAME bitset +#define PFX bs +#include "cmc/bitset.h" #define SNAME deque -#define CMC_EXT_DEQUE_ITER +#define PFX d #include "cmc/deque.h" +#define SNAME hashbidimap +#define PFX hbm +#include "cmc/hashbidimap.h" +#define SNAME hashmap +#define PFX hm +#include "cmc/hashmap.h" +#define SNAME hashmultimap +#define PFX hmm +#include "cmc/hashmultimap.h" +#define SNAME hashmultiset +#define PFX hms +#include "cmc/hashmultiset.h" +#define SNAME hashset +#define PFX hs +#include "cmc/hashset.h" +#define SNAME heap +#define PFX h +#include "cmc/heap.h" +#define SNAME intervalheap +#define PFX ih +#include "cmc/intervalheap.h" +#define SNAME linkedlist +#define PFX ll +#include "cmc/linkedlist.h" +#define SNAME list +#define PFX l +#include "cmc/list.h" +#define SNAME queue +#define PFX q +#include "cmc/queue.h" +#define SNAME sortedlist +#define PFX sl +#include "cmc/sortedlist.h" +#define SNAME stack +#define PFX s +#include "cmc/stack.h" +#define SNAME treemap +#define PFX tm +#include "cmc/treemap.h" +#define SNAME treeset +#define PFX ts +#include "cmc/treeset.h" -// CMC_COLLECTION_GENERATE(DEQUE, d, deque, /* K */, int) -// CMC_COLLECTION_GENERATE(HASHMAP, hm, hmap, int, int) -// CMC_COLLECTION_GENERATE(HASHSET, hs, hset, /* K */, int) -// CMC_COLLECTION_GENERATE(HEAP, h, heap, /* K */, int) -// CMC_COLLECTION_GENERATE(LINKEDLIST, ll, linked, /* K */, int) -// CMC_COLLECTION_GENERATE(LIST, l, list, /* K */, int) -// CMC_COLLECTION_GENERATE(QUEUE, q, queue, /* K */, int) -// CMC_COLLECTION_GENERATE(STACK, s, stack, /* K */, int) -// CMC_COLLECTION_GENERATE(TREEMAP, tm, tmap, int, int) -// CMC_COLLECTION_GENERATE(TREESET, ts, tset, /* K */, int) +#undef CMC_ARGS_FALLTHROUGH +#include "cmc/cor/undef.h" -// CMC_COLLECTION_GENERATE(INTERVALHEAP, ih, iheap, /* k */, int) -// CMC_COLLECTION_GENERATE(MULTIMAP, mm, mmap, int, int) -// CMC_COLLECTION_GENERATE(MULTISET, ms, mset, /* K */, int) - -#define BENCHMARK(NAME, PFX, sname, initfunc, insertbody, removebody, searchbody) \ +#define BENCHMARK(NAME, PFX, SNAME, initfunc, insertbody, removebody, searchbody) \ void NAME##_io_benchmark(int *array, int *sarray, size_t s) \ { \ printf("+------------------------------------------------------------ %10s\n", #NAME); \ printf("+------------------------------------------------------------ \n"); \ struct cmc_timer timer, total; \ - int r, k, v; \ - size_t j; \ + CMC_UNUSED int r; \ \ - struct sname *coll = initfunc; \ + struct SNAME *coll = initfunc; \ \ cmc_timer_start(total); \ cmc_timer_start(timer); \ @@ -127,7 +156,7 @@ void shuffle(int *array, mt_state_ptr st) \ cmc_timer_start(timer); \ \ - CMC_FOREACH (PFX, sname, iter, coll) \ + CMC_FOREACH (PFX, SNAME, iter, coll) \ { \ } \ \ @@ -175,33 +204,34 @@ void shuffle(int *array, mt_state_ptr st) printf("+------------------------------------------------------------ %10s\n\n", #NAME); \ } -BENCHMARK(DEQUE, d, deque, d_new(NTOTAL, &(struct deque_fval){ .cmp = cmc_i32_cmp }), d_push_back(coll, array[i]), - d_pop_back(coll), d_contains(coll, sarray[i])) -// BENCHMARK(HASHMAP, hm, hmap, hm_new(NTOTAL, 0.6, intcmp, inthash), hm_insert(coll, array[i], array[i]), -// hm_remove(coll, array[i], &r), hm_contains(coll, sarray[i])) -// BENCHMARK(HASHSET, hs, hset, hs_new(NTOTAL, 0.6, intcmp, inthash), hs_insert(coll, array[i]), hs_remove(coll, -// array[i]), -// hs_contains(coll, sarray[i])) -// BENCHMARK(HEAP, h, heap, h_new(NTOTAL, cmc_min_heap, intcmp), h_insert(coll, array[i]), h_remove(coll, &r), -// h_contains(coll, sarray[i])) -// BENCHMARK(LINKEDLIST, ll, linked, ll_new(), ll_push_back(coll, array[i]), ll_pop_back(coll), -// ll_contains(coll, sarray[i], intcmp)) -// BENCHMARK(LIST, l, list, l_new(NTOTAL), l_push_back(coll, array[i]), l_pop_back(coll), -// l_contains(coll, sarray[i], intcmp)) -// BENCHMARK(QUEUE, q, queue, q_new(NTOTAL), q_enqueue(coll, array[i]), q_dequeue(coll), -// q_contains(coll, sarray[i], intcmp)) -// BENCHMARK(STACK, s, stack, s_new(NTOTAL), s_push(coll, array[i]), s_pop(coll), s_contains(coll, sarray[i], intcmp)) -// BENCHMARK(TREEMAP, tm, tmap, tm_new(intcmp), tm_insert(coll, array[i], array[i]), tm_remove(coll, array[i], &r), -// tm_contains(coll, sarray[i])) -// BENCHMARK(TREESET, ts, tset, ts_new(intcmp), ts_insert(coll, array[i]), ts_remove(coll, array[i]), -// ts_contains(coll, sarray[i])) +BENCHMARK(DEQUE, d, deque, d_new(NTOTAL, (struct deque_fval *)&int_ftab), d_push_back(coll, array[i]), d_pop_back(coll), + d_contains(coll, sarray[i])) +BENCHMARK(HASHMAP, hm, hashmap, hm_new(NTOTAL, 0.6, (struct hashmap_fkey *)&int_ftab, (struct hashmap_fval *)&int_ftab), + hm_insert(coll, array[i], array[i]), hm_remove(coll, array[i], &r), hm_contains(coll, sarray[i])) +BENCHMARK(HASHSET, hs, hashset, hs_new(NTOTAL, 0.6, (struct hashset_fval *)&int_ftab), hs_insert(coll, array[i]), + hs_remove(coll, array[i]), hs_contains(coll, sarray[i])) +BENCHMARK(HEAP, h, heap, h_new(NTOTAL, CMC_MIN_HEAP, (struct heap_fval *)&int_ftab), h_insert(coll, array[i]), + h_remove(coll), h_contains(coll, sarray[i])) +BENCHMARK(LINKEDLIST, ll, linkedlist, ll_new((struct linkedlist_fval *)&int_ftab), ll_push_back(coll, array[i]), + ll_pop_back(coll), ll_contains(coll, sarray[i])) +BENCHMARK(LIST, l, list, l_new(NTOTAL, (struct list_fval *)&int_ftab), l_push_back(coll, array[i]), l_pop_back(coll), + l_contains(coll, sarray[i])) +BENCHMARK(QUEUE, q, queue, q_new(NTOTAL, (struct queue_fval *)&int_ftab), q_enqueue(coll, array[i]), q_dequeue(coll), + q_contains(coll, sarray[i])) +BENCHMARK(STACK, s, stack, s_new(NTOTAL, (struct stack_fval *)&int_ftab), s_push(coll, array[i]), s_pop(coll), + s_contains(coll, sarray[i])) +BENCHMARK(TREEMAP, tm, treemap, tm_new((struct treemap_fkey *)&int_ftab, (struct treemap_fval *)&int_ftab), + tm_insert(coll, array[i], array[i]), tm_remove(coll, array[i], &r), tm_contains(coll, sarray[i])) +BENCHMARK(TREESET, ts, treeset, ts_new((struct treeset_fval *)&int_ftab), ts_insert(coll, array[i]), + ts_remove(coll, array[i]), ts_contains(coll, sarray[i])) -// BENCHMARK(INTERVALHEAP, ih, iheap, ih_new(NTOTAL, intcmp), ih_insert(coll, array[i]), ih_remove_max(coll, &r), -// ih_contains(coll, sarray[i])) -// BENCHMARK(MULTIMAP, mm, mmap, mm_new(NTOTAL, 0.8, intcmp, inthash), mm_insert(coll, array[i], array[i]), -// mm_remove(coll, array[i], &r), mm_contains(coll, sarray[i])) -// BENCHMARK(MULTISET, ms, mset, ms_new(NTOTAL, 0.6, intcmp, inthash), ms_insert(coll, array[i]), -// ms_remove(coll, array[i]), ms_contains(coll, sarray[i])) +BENCHMARK(INTERVALHEAP, ih, intervalheap, ih_new(NTOTAL, (struct intervalheap_fval *)&int_ftab), + ih_insert(coll, array[i]), ih_remove_max(coll), ih_contains(coll, sarray[i])) +BENCHMARK(MULTIMAP, hmm, hashmultimap, + hmm_new(NTOTAL, 0.8, (struct hashmultimap_fkey *)&int_ftab, (struct hashmultimap_fval *)&int_ftab), + hmm_insert(coll, array[i], array[i]), hmm_remove(coll, array[i], &r), hmm_contains(coll, sarray[i])) +BENCHMARK(MULTISET, hms, hashmultiset, hms_new(NTOTAL, 0.6, (struct hashmultiset_fval *)&int_ftab), + hms_insert(coll, array[i]), hms_remove(coll, array[i]), hms_contains(coll, sarray[i])) int main(void) { @@ -239,19 +269,19 @@ int main(void) cmc_timer_start(timer); DEQUE_io_benchmark(array, sarray, NMIN); - // HASHMAP_io_benchmark(array, sarray, NTOTAL); - // HASHSET_io_benchmark(array, sarray, NTOTAL); - // HEAP_io_benchmark(array, sarray, NMIN); - // LINKEDLIST_io_benchmark(array, sarray, NMIN); - // LIST_io_benchmark(array, sarray, NMIN); - // QUEUE_io_benchmark(array, sarray, NMIN); - // STACK_io_benchmark(array, sarray, NMIN); - // TREEMAP_io_benchmark(array, sarray, NTOTAL); - // TREESET_io_benchmark(array, sarray, NTOTAL); + HASHMAP_io_benchmark(array, sarray, NTOTAL); + HASHSET_io_benchmark(array, sarray, NTOTAL); + HEAP_io_benchmark(array, sarray, NMIN); + LINKEDLIST_io_benchmark(array, sarray, NMIN); + LIST_io_benchmark(array, sarray, NMIN); + QUEUE_io_benchmark(array, sarray, NMIN); + STACK_io_benchmark(array, sarray, NMIN); + TREEMAP_io_benchmark(array, sarray, NTOTAL); + TREESET_io_benchmark(array, sarray, NTOTAL); - // INTERVALHEAP_io_benchmark(array, array, NMIN); - // MULTIMAP_io_benchmark(array, array, NTOTAL); - // MULTISET_io_benchmark(array, array, NTOTAL); + // INTERVALHEAP_io_benchmark(array, array, NMIN); + // MULTIMAP_io_benchmark(array, array, NTOTAL); + // MULTISET_io_benchmark(array, array, NTOTAL); cmc_timer_stop(timer); diff --git a/src/cmc/cmc/hashmultiset/code.h b/src/cmc/cmc/hashmultiset/code.h index db50f0f..344d72f 100644 --- a/src/cmc/cmc/hashmultiset/code.h +++ b/src/cmc/cmc/hashmultiset/code.h @@ -23,7 +23,6 @@ */ /* Implementation Detail Functions */ -static size_t CMC_(PFX, _impl_multiplicity_of)(struct SNAME *_set_, V value); static struct CMC_DEF_ENTRY(SNAME) * CMC_(PFX, _impl_insert_and_return)(struct SNAME *_set_, V value, bool *new_node); static struct CMC_DEF_ENTRY(SNAME) * CMC_(PFX, _impl_get_entry)(struct SNAME *_set_, V value); static size_t CMC_(PFX, _impl_calculate_size)(size_t required); @@ -598,16 +597,6 @@ bool CMC_(PFX, _equals)(struct SNAME *_set1_, struct SNAME *_set2_) return true; } -static size_t CMC_(PFX, _impl_multiplicity_of)(struct SNAME *_set_, V value) -{ - struct CMC_DEF_ENTRY(SNAME) *entry = CMC_(PFX, _impl_get_entry)(_set_, value); - - if (!entry) - return 0; - - return entry->multiplicity; -} - static struct CMC_DEF_ENTRY(SNAME) * CMC_(PFX, _impl_insert_and_return)(struct SNAME *_set_, V value, bool *new_node) { /* If the entry already exists simply return it as we might do */ diff --git a/src/cmc/cmc/hashmultiset/ext/code.h b/src/cmc/cmc/hashmultiset/ext/code.h index 945679d..eb9cd4a 100644 --- a/src/cmc/cmc/hashmultiset/ext/code.h +++ b/src/cmc/cmc/hashmultiset/ext/code.h @@ -301,6 +301,9 @@ size_t CMC_(PFX, _iter_index)(struct CMC_DEF_ITER(SNAME) * iter) */ #ifdef CMC_EXT_SETF +/* Implementation detail function */ +static size_t CMC_(PFX, _impl_multiplicity_of)(struct SNAME *_set_, V value); + struct SNAME *CMC_(PFX, _union)(struct SNAME *_set1_, struct SNAME *_set2_) { /* Callbacks are added later */ @@ -550,6 +553,16 @@ bool CMC_(PFX, _is_disjointset)(struct SNAME *_set1_, struct SNAME *_set2_) return true; } +static size_t CMC_(PFX, _impl_multiplicity_of)(struct SNAME *_set_, V value) +{ + struct CMC_DEF_ENTRY(SNAME) *entry = CMC_(PFX, _impl_get_entry)(_set_, value); + + if (!entry) + return 0; + + return entry->multiplicity; +} + #endif /* CMC_EXT_SETF */ /**