From 91a602f774f1921c618f56a3f8aaa908f7ba42c9 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Tue, 19 Dec 2023 20:58:02 +0100 Subject: [PATCH] Add bench of boost unordered flat hashmap --- bench/Makefile | 7 ++- bench/bench-boost.cpp | 116 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 3 deletions(-) diff --git a/bench/Makefile b/bench/Makefile index 66893b94..183bcdc2 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -322,8 +322,11 @@ bench-xxhash0: bench-boost: @if test -n "$${BOOST}" ; then $(MAKE) bench-boost0 ; else echo "Nothing to be done for BOOST." ; fi bench-boost0: - $(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I "$${BOOST}" bench-boost.cpp common.c -DMULTI_THREAD_MEASURE -o bench-boost.exe -lpthread - @./bench-boost.exe 60 + $(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I "$${BOOST}" bench-boost.cpp common.c -DMULTI_THREAD_MEASURE -o bench-boost-thread.exe -lpthread + $(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I "$${BOOST}" bench-boost.cpp common.c -o bench-boost.exe -lpthread + @./bench-boost.exe 42 + @./bench-boost.exe 41 + @./bench-boost-thread.exe 60 # NEDTRIES shall point to the header location. bench-nedtries: diff --git a/bench/bench-boost.cpp b/bench/bench-boost.cpp index c83ff1d6..e4fe01e9 100644 --- a/bench/bench-boost.cpp +++ b/bench/bench-boost.cpp @@ -3,11 +3,121 @@ #include #include #include +#include "m-thread.h" +#include #include #include "common.h" +using namespace std; + +/********************************************************************************************/ + +static void +test_dict2(size_t n) +{ + boost::unordered_flat_map dict; + for (size_t i = 0; i < n; i++) { + dict[rand_get()] = rand_get(); + } + rand_init(); + unsigned int s = 0; + for (size_t i = 0; i < n; i++) { + boost::unordered_flat_map::iterator it = dict.find(rand_get()); + if (it != dict.end()) + s += it->second; + } + g_result = s; +} + +static void +test_dict2_linear(size_t n) +{ + boost::unordered_flat_map dict; + for (size_t i = 0; i < n; i++) { + dict[i] = rand_get(); + } + rand_init(); + unsigned int s = 0; + for (size_t i = 0; i < n; i++) { + boost::unordered_flat_map::iterator it = dict.find(i); + if (it != dict.end()) + s += it->second; + } + g_result = s; +} + +/********************************************************************************************/ +struct char_array_s { + char a[256]; + char_array_s () { a[0] = 0 ; } + char_array_s ( const char_array_s & other) { strcpy(a, other.a); } + bool operator==(const char_array_s &other) const { return strcmp(a, other.a) == 0; } +}; + +inline std::size_t hash_value(const char_array_s &k) { + size_t hash = 0; + const char *s = k.a; + while (*s) hash = hash * 31421 + (*s++) + 6927; + return hash; +} + +struct Hash32 { + //using is_avalanching = void; + inline std::size_t operator()(const char_array_s &k) const { + size_t hash = 0; + const char *s = k.a; + while (*s) hash = hash * 31421 + (*s++) + 6927; + return hash; + }; +}; + +static void +test_dict_big(size_t n) +{ + boost::unordered_flat_map dict; + + for (size_t i = 0; i < n; i++) { + char_array_s s1, s2; + sprintf(s1.a, "%u", rand_get()); + sprintf(s2.a, "%u", rand_get()); + dict[s1] = s2; + } + rand_init(); + unsigned int s = 0; + for (size_t i = 0; i < n; i++) { + char_array_s s1; + sprintf(s1.a, "%u", rand_get()); + boost::unordered_flat_map::iterator it = dict.find(s1); + if (it != dict.end()) + s ++; + } + g_result = s; +} + +/********************************************************************************************/ +static void +test_dict_str(size_t n) +{ + boost::unordered_flat_map dict; + + for (size_t i = 0; i < n; i++) { + string s1 = static_cast( &(ostringstream() << rand_get()) )->str(); + string s2 = static_cast( &(ostringstream() << rand_get()) )->str(); + dict[s1] = s2; + } + rand_init(); + unsigned int s = 0; + for (size_t i = 0; i < n; i++) { + string s1 = static_cast( &(ostringstream() << rand_get()) )->str(); + boost::unordered_flat_map::iterator it = dict.find(s1); + if (it != dict.end()) + s ++; + } + g_result = s; +} + /********************************************************************************************/ boost::lockfree::queue> g_buff; boost::lockfree::queue> g_final; @@ -86,11 +196,15 @@ static void test_queue(size_t n) /********************************************************************************************/ const config_func_t table[] = { + { 41, "dictBig", 1000000, 0, test_dict_big, 0}, + { 42, "dict", 1000000, 0, test_dict2, 0}, + { 43, "DictStr", 1000000, 0, test_dict_str, 0}, + { 46, "DictLinear", 1000000, 0, test_dict2_linear, 0}, { 60, "Queue MPMC", 1000000, 0, test_queue, 0} }; int main(int argc, const char *argv[]) { - test("BOOST-LOCKFREE", numberof(table), table, argc, argv); + test("BOOST", numberof(table), table, argc, argv); exit(0); }