From 35a391c029ae2070161bb3a66e9f9fbe859214d6 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Thu, 10 Aug 2017 23:28:52 +0200 Subject: [PATCH] Add bench of google dense hash map. --- bench/Makefile | 10 +++ bench/bench-densehashmap.cpp | 144 +++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 bench/bench-densehashmap.cpp diff --git a/bench/Makefile b/bench/Makefile index a0d9b565..32fcc4c3 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -98,6 +98,16 @@ bench-sparsepp0: @./bench-sparsepp.exe 42 @./bench-sparsepp.exe 43 +# DENSEHASHMAP shall point to the directory where the src are +bench-densehashmap: + if test -n "$${DENSEHASHMAP}" ; then make bench-densehashmap0 ; else echo "Nothing to be done." ; fi + +bench-densehashmap0: + $(CXX) $(CFLAGS) $(CPPFLAGS) -I "$${DENSEHASHMAP}" -I "$${DENSEHASHMAP}/.." bench-densehashmap.cpp -o bench-densehashmap.exe + @./bench-densehashmap.exe 41 + @./bench-densehashmap.exe 42 + @./bench-densehashmap.exe 43 + # COLLECTIONC shall point to the directory where the 'src' directory is. bench-collectionc: if test -n "$${COLLECTIONC}" ; then make bench-collectionc0 ; else echo "Nothing to be done." ; fi diff --git a/bench/bench-densehashmap.cpp b/bench/bench-densehashmap.cpp new file mode 100644 index 00000000..856b19b4 --- /dev/null +++ b/bench/bench-densehashmap.cpp @@ -0,0 +1,144 @@ +#define NDEBUG + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +using google::dense_hash_map; +using namespace std; + +#include "common.h" + +/********************************************************************************************/ + +static void +test_dict2(unsigned long n) +{ + dense_hash_map dict; + dict.set_empty_key(-1); + dict.set_deleted_key(-2); + + 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++) { + dense_hash_map::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; + }; + }; + namespace tr1 { + template <> struct hash { + std::size_t operator()(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(unsigned long n) +{ + dense_hash_map dict; + struct char_array_s empty, del; + memset(&empty, 0, sizeof empty); empty.a[1] = 255; + memset(&del, 0, sizeof del); del.a[1] = 255; + dict.set_empty_key(empty); + dict.set_deleted_key(del); + + 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()); + dense_hash_map::iterator it = dict.find(s1); + if (it != dict.end()) + s ++; + } + g_result = s; +} + +/********************************************************************************************/ +static void +test_dict_str(unsigned long n) +{ + dense_hash_map dict; + string empty, del; + empty = ""; + del = "DEL"; + dict.set_empty_key(empty); + dict.set_deleted_key(del); + + 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(); + dense_hash_map::iterator it = dict.find(s1); + if (it != dict.end()) + s ++; + } + g_result = s; +} + +/********************************************************************************************/ + +int main(int argc, const char *argv[]) +{ + int n = (argc > 1) ? atoi(argv[1]) : 0; + if (n == 42) + test_function("Dict(u)time", 1000000, test_dict2); + if (n == 41) + test_function("DictB time", 1000000, test_dict_big); + if (n == 43) + test_function("DictS(u) time", 1000000, test_dict_str); + exit(0); +}