From 4de7da1c052885586686188e257d164ecc662efe Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 15 Oct 2022 10:19:33 +0200 Subject: [PATCH] Adding bench for emhash https://github.com/ktprime/emhash --- bench/Makefile | 15 ++++- bench/bench-emhash.cpp | 138 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 bench/bench-emhash.cpp diff --git a/bench/Makefile b/bench/Makefile index 065b65e9..6e639b03 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -29,7 +29,7 @@ N=4 BENCH_DEF= RM=rm -rf -.PHONY: all pgo container queue mempool string plain bench-mlib bench-mlib-mempool bench-stl bench-qt bench-glib bench-klib bench-libdynamic bench-sparsepp bench-collectionc bench-tommyds bench-flathashmap bench-emilib bench-hopscotchmap bench-mlib-thread bench-liblfds bench-concurrentqueue bench-boost bench-mempool bench-string bench-plain bench-rigtorp-mpmc-queue bench-cmc +.PHONY: all pgo container queue mempool string plain bench-mlib bench-mlib-mempool bench-stl bench-qt bench-glib bench-klib bench-libdynamic bench-sparsepp bench-collectionc bench-tommyds bench-flathashmap bench-emilib bench-emhash bench-hopscotchmap bench-mlib-thread bench-liblfds bench-concurrentqueue bench-boost bench-mempool bench-string bench-plain bench-rigtorp-mpmc-queue bench-cmc all: container queue mempool string plain @@ -39,7 +39,7 @@ pgo: ######################################################################### -container: bench-mlib bench-mlib-mempool bench-stl bench-qt bench-glib bench-klib bench-libdynamic bench-cmc bench-sparsepp bench-densehashmap bench-collectionc bench-tommyds bench-flathashmap bench-emilib bench-hopscotchmap bench-uthash bench-qlibc bench-libsrt bench-nedtries bench-rigtorp-hashmap bench-ctl bench-stc bench-pottery +container: bench-mlib bench-mlib-mempool bench-stl bench-qt bench-glib bench-klib bench-libdynamic bench-cmc bench-sparsepp bench-densehashmap bench-collectionc bench-tommyds bench-flathashmap bench-emilib bench-emhash bench-hopscotchmap bench-uthash bench-qlibc bench-libsrt bench-nedtries bench-rigtorp-hashmap bench-ctl bench-stc bench-pottery @echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" queue: bench-mlib-thread bench-liblfds bench-concurrentqueue bench-boost bench-rigtorp-mpmc-queue @@ -239,6 +239,17 @@ bench-emilib0: @./bench-emilib.exe 46 @./bench-emilib.exe 43 +# EMHASh shall point to the directory where the headers are +bench-emhash: + @if test -n "$${EMHASH}" ; then $(MAKE) bench-emhash0 ; else echo "Nothing to be done for EMHASH." ; fi + +bench-emhash0: + $(CXX) -std=c++14 $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I "$${EMHASH}" bench-emhash.cpp common.c -o bench-emhash.exe + @./bench-emhash.exe 41 + @./bench-emhash.exe 42 + @./bench-emhash.exe 46 + @./bench-emhash.exe 43 + # HOPSCOTCHMAP shall point to the directory where the source are bench-hopscotchmap: @if test -n "$${HOPSCOTCHMAP}" ; then $(MAKE) bench-hopscotchmap0 ; else echo "Nothing to be done for HOPSCOTCHMAP." ; fi diff --git a/bench/bench-emhash.cpp b/bench/bench-emhash.cpp new file mode 100644 index 00000000..3c90357b --- /dev/null +++ b/bench/bench-emhash.cpp @@ -0,0 +1,138 @@ +#define NDEBUG + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "hash_table7.hpp" + +using namespace emhash7; +using namespace std; + +#include "common.h" + +/********************************************************************************************/ + +static void +test_dict2(size_t n) +{ + HashMap 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++) { + HashMap::iterator it = dict.find(rand_get()); + if (it != dict.end()) + s += it->second; + } + g_result = s; +} + +static void +test_dict2_linear(size_t n) +{ + HashMap 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++) { + HashMap::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; } +}; + +namespace std { + template <> struct hash { + 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) +{ + HashMap 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()); + HashMap::iterator it = dict.find(s1); + if (it != dict.end()) + s ++; + } + g_result = s; +} + +/********************************************************************************************/ +static void +test_dict_str(size_t n) +{ + HashMap 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(); + HashMap::iterator it = dict.find(s1); + if (it != dict.end()) + s ++; + } + g_result = s; +} + +/********************************************************************************************/ + +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} +}; + +int main(int argc, const char *argv[]) +{ + test("EMHASH", numberof(table), table, argc, argv); + exit(0); +}