From c3d4342e25ae670797f7e19f9a416de0a1a3cd1c Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 25 Mar 2017 12:35:36 +0100 Subject: [PATCH] Add big dict bench --- bench/Makefile | 3 +++ bench/bench-klib.c | 45 ++++++++++++++++++++++++++++++++++++++++++++ bench/bench-mlib.c | 42 +++++++++++++++++++++++++++++++++++++++++ bench/bench-stl.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) diff --git a/bench/Makefile b/bench/Makefile index 037b2ab6..7c2392f8 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -11,6 +11,7 @@ bench-mlib: @./bench-mlib.exe 2 @./bench-mlib.exe 3 @./bench-mlib.exe 4 + @./bench-mlib.exe 6 bench-stl: $(CXX) $(CFLAGS) $(CPPFLAGS) bench-stl.cpp -o bench-stl.exe @@ -19,6 +20,7 @@ bench-stl: @./bench-stl.exe 3 @./bench-stl.exe 4 @./bench-stl.exe 5 + @./bench-stl.exe 6 bench-klib: $(CC) $(CFLAGS) $(CPPFLAGS) $${KLIB_CPPFLAGS} bench-klib.c -o bench-klib.exe @@ -26,6 +28,7 @@ bench-klib: @./bench-klib.exe 2 @./bench-klib.exe 3 @./bench-klib.exe 4 + @./bench-klib.exe 6 clean: rm -f *.o bench-*.exe *~ diff --git a/bench/bench-klib.c b/bench/bench-klib.c index cd2e4cdf..9e090b35 100644 --- a/bench/bench-klib.c +++ b/bench/bench-klib.c @@ -149,6 +149,49 @@ test_dict(unsigned long n) /********************************************************************************************/ +typedef struct { + char a[256]; +} char_array_t; +static inline void char_init (char_array_t *a) { a->a[0] = 0; } +static inline void char_set (char_array_t *a, const char_array_t b) { strcpy(a->a, b.a); } +static inline bool char_equal_p (const char_array_t a, const char_array_t b) { return strcmp(a.a,b.a)==0; } +static inline size_t char_hash(const char_array_t a) { + size_t hash = 0; + const char *s = a.a; + while (*s) hash = hash * 31421 + (*s++) + 6927; + return hash; +} + +KHASH_INIT(iub, char_array_t, char_array_t, 1, char_hash, char_equal_p) + +static void +test_dict_big(unsigned long n) +{ + rand_init(); + khash_t(iub) *dict = kh_init(iub); + + for (size_t i = 0; i < n; i++) { + int ret; + char_array_t s1, s2; + sprintf(s1.a, "%u", rand_get()); + sprintf(s2.a, "%u", rand_get()); + khiter_t k = kh_put(iub, dict, s1, &ret); + char_set(&kh_value(dict, k), s2); + } + unsigned int s = 0; + for (size_t i = 0; i < n; i++) { + char_array_t s1; + sprintf(s1.a, "%u", rand_get()); + khiter_t k = kh_get(iub, dict, s1); + if (kh_exist(dict, k)) + s ++; + } + + kh_destroy(iub, dict); +} + +/********************************************************************************************/ + int main(int argc, const char *argv[]) { int n = (argc > 1) ? atoi(argv[1]): 0; @@ -160,5 +203,7 @@ int main(int argc, const char *argv[]) test_function("B-tree time", 1000000, test_rbtree); if (n == 4) test_function("Dict time", 1000000, test_dict); + if (n == 6) + test_function("DictB time", 1000000, test_dict_big); } diff --git a/bench/bench-mlib.c b/bench/bench-mlib.c index f9546f8c..d7f0d57c 100644 --- a/bench/bench-mlib.c +++ b/bench/bench-mlib.c @@ -126,6 +126,46 @@ test_dict(unsigned long n) /********************************************************************************************/ +typedef char char_array_t[256]; + +static void char_init (char_array_t a) { a[0] = 0; } +static void char_set (char_array_t a, const char_array_t b) { strcpy(a, b); } +static bool char_equal_p (const char_array_t a, const char_array_t b) { return strcmp(a,b)==0; } +static size_t char_hash(const char_array_t a) { + M_HASH_DECL(hash); + const char *s = a; + while (*s) M_HASH_UP(hash, *s++); + return hash; +} +// NOTE: Can't set OPLIST as a macro! +#define CHAR_OPLIST (INIT(char_init), INIT_SET(char_set), SET(char_set), CLEAR(char_init), HASH(char_hash), EQUAL(char_equal_p)) + +DICT_DEF2(char, char_array_t, CHAR_OPLIST, char_array_t, CHAR_OPLIST) + +static void +test_dict_big(unsigned long n) +{ + rand_init(); + M_LET(dict, DICT_OPLIST(char)) { + for (size_t i = 0; i < n; i++) { + char_array_t s1, s2; + sprintf(s1, "%u", rand_get()); + sprintf(s2, "%u", rand_get()); + dict_char_set_at(dict, s1, s2); + } + unsigned int s = 0; + for (size_t i = 0; i < n; i++) { + char_array_t s1; + sprintf(s1, "%u", rand_get()); + char_array_t *p = dict_char_get(dict, s1); + if (p) + s ++; + } + } +} + +/********************************************************************************************/ + int main(int argc, const char *argv[]) { int n = (argc > 1) ? atoi(argv[1]) : 0; @@ -137,5 +177,7 @@ int main(int argc, const char *argv[]) test_function("Rbtree time", 1000000, test_rbtree); if (n == 4) test_function("Dict time", 1000000, test_dict); + if (n == 6) + test_function("DictB time", 1000000, test_dict_big); } diff --git a/bench/bench-stl.cpp b/bench/bench-stl.cpp index 55587e20..0950b838 100644 --- a/bench/bench-stl.cpp +++ b/bench/bench-stl.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -139,6 +140,49 @@ test_dict2(unsigned long n) /********************************************************************************************/ +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(unsigned long n) +{ + rand_init(); + unordered_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; + } + + unsigned int s = 0; + for (size_t i = 0; i < n; i++) { + char_array_s s1; + sprintf(s1.a, "%u", rand_get()); + unordered_map::iterator it = dict.find(s1); + if (it != dict.end()) + s ++; + } +} + +/********************************************************************************************/ + int main(int argc, const char *argv[]) { int n = (argc > 1) ? atoi(argv[1]) : 0; @@ -152,4 +196,6 @@ int main(int argc, const char *argv[]) test_function("Dict(m)time", 1000000, test_dict1); if (n == 5) test_function("Dict(u)time", 1000000, test_dict2); + if (n == 6) + test_function("DictB time", 1000000, test_dict_big); }