Add big dict bench

This commit is contained in:
Patrick Pelissier committed 2017-03-25 12:35:36 +01:00
1 parent b0b70c6e5d
commit c3d4342e25
4 files changed
+136

No files matched your search

+3
View File
@@ -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 *~
+45
View File
@@ -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);
}
+42
View File
@@ -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);
}
+46
View File
@@ -8,6 +8,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/time.h>
@@ -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<char_array_s> {
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<char_array_s, char_array_s> 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<char_array_s, char_array_s>::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);
}