From 7263629c553935804559c39fe213f6fe93a93893 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 14 Dec 2019 20:59:01 +0100 Subject: [PATCH] Add rigtop hashmap --- bench/Makefile | 10 ++ bench/bench-rigtorp-hashmap.cpp | 159 ++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 bench/bench-rigtorp-hashmap.cpp diff --git a/bench/Makefile b/bench/Makefile index da39484a..20932800 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -287,6 +287,16 @@ bench-nedtries0: @./bench-nedtries.exe 40 @./bench-nedtries.exe 45 +# RIGTORP_HASHMAP shall point to the directory where the headers are +bench-rigtorp-hashmap: + @if test -n "$${RIGTORP_HASHMAP}" ; then $(MAKE) bench-rigtorp-hashmap0 ; else echo "Nothing to be done for RIGTORP_HASHMAP." ; fi + +bench-rigtorp-hashmap0: + $(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I "$${RIGTORP_HASHMAP}/include" bench-rigtorp-hashmap.cpp common.c -o bench-rigtorp-hashmap.exe + @./bench-rigtorp-hashmap.exe 42 + @./bench-rigtorp-hashmap.exe 41 + @./bench-rigtorp-hashmap.exe 43 + ############################################################################ bench-plain: diff --git a/bench/bench-rigtorp-hashmap.cpp b/bench/bench-rigtorp-hashmap.cpp new file mode 100644 index 00000000..2d33d3c6 --- /dev/null +++ b/bench/bench-rigtorp-hashmap.cpp @@ -0,0 +1,159 @@ +#define NDEBUG + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "rigtorp/HashMap.h" + +using namespace rigtorp; +using namespace std; + +#include "common.h" + +/********************************************************************************************/ + +// Hash for using unsigned long as lookup key +struct UL_Hash { + size_t operator()(const unsigned long v) { return v * 7; } +}; + +// Equal comparison for using unsigned long as lookup key +struct UL_Equal { + bool operator()(const unsigned long lhs, const unsigned long rhs) { + return lhs == rhs; + } +}; + +static void +test_dict2(size_t n) +{ + HashMap dict(16, -1UL); + + 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; +} + +/********************************************************************************************/ + + +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; + }; + }; +} + +struct BIG_Hash { + size_t operator()(const char_array_s v) { return hash()(v); } +}; + +struct BIG_Equal { + bool operator()(const char_array_s lhs, const char_array_s rhs) { + return lhs == rhs; + } +}; + +static void +test_dict_big(size_t n) +{ + char_array_s empty; + HashMap dict(16, empty); + + 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; +} + +/********************************************************************************************/ + +struct STR_Hash { + size_t operator()(const string v) { return hash()(v); } +}; + +struct STR_Equal { + bool operator()(const string lhs, const string rhs) { + return lhs == rhs; + } +}; + + +static void +test_dict_str(size_t n) +{ + string empty; + HashMap dict(16, empty); + + 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} +}; + +int main(int argc, const char *argv[]) +{ + test("RIGTORP-HASHMAP", numberof(table), table, argc, argv); + exit(0); +}