diff --git a/bench/Makefile b/bench/Makefile index 2a64fca5..c8cae09f 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -86,6 +86,16 @@ bench-libdynamic0: @./bench-libdynamic.exe 41 @./bench-libdynamic.exe 50 +# SPARSEPP shall point to the directory where the headers are +bench-sparsepp: + if test -n "$${SPARSEPP}" ; then make bench-sparsepp0 ; else echo "Nothing to be done." ; fi + +bench-sparsepp0: + $(CXX) $(CFLAGS) $(CPPFLAGS) -I "$${SPARSEPP}" -I "$${SPARSEPP}/.." bench-sparsepp.cpp -o bench-sparsepp.exe + @./bench-sparsepp.exe 41 + @./bench-sparsepp.exe 42 + @./bench-sparsepp.exe 43 + bench-plain: $(CC) $(CFLAGS) $(CPPFLAGS) bench-plain-c.c -o bench-plain-c.exe @./bench-plain-c.exe diff --git a/bench/bench-sparsepp.cpp b/bench/bench-sparsepp.cpp new file mode 100644 index 00000000..2c49a25f --- /dev/null +++ b/bench/bench-sparsepp.cpp @@ -0,0 +1,125 @@ +#define NDEBUG + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +using spp::sparse_hash_map; +using namespace std; + +#include "common.h" + +/********************************************************************************************/ + +static void +test_dict2(unsigned long n) +{ + sparse_hash_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++) { + sparse_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; + }; + }; +} + +static void +test_dict_big(unsigned long n) +{ + sparse_hash_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()); + sparse_hash_map::iterator it = dict.find(s1); + if (it != dict.end()) + s ++; + } + g_result = s; +} + +/********************************************************************************************/ +static void +test_dict_str(unsigned long n) +{ + sparse_hash_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(); + sparse_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); +}