summaryrefslogtreecommitdiff
path: root/legacy/eina/src/tests/eina_bench_hash.c
diff options
context:
space:
mode:
authorCedric BAIL <cedric.bail@free.fr>2008-08-07 09:13:24 +0000
committerCedric BAIL <cedric.bail@free.fr>2008-08-07 09:13:24 +0000
commitd2f17c1e8145a23fc973459c9e6b9e8c2217cf30 (patch)
treefbd2e993260287cee7d1c41bc08c7b252db7239a /legacy/eina/src/tests/eina_bench_hash.c
parentc66618b521ee251a5e0745683674e840da82d823 (diff)
Benchmark inline version of djb2 hash function.
SVN revision: 35376
Diffstat (limited to '')
-rw-r--r--legacy/eina/src/tests/eina_bench_hash.c70
1 files changed, 20 insertions, 50 deletions
diff --git a/legacy/eina/src/tests/eina_bench_hash.c b/legacy/eina/src/tests/eina_bench_hash.c
index 816aaa6d40..d23124bec6 100644
--- a/legacy/eina/src/tests/eina_bench_hash.c
+++ b/legacy/eina/src/tests/eina_bench_hash.c
@@ -26,7 +26,7 @@
26#include "eina_bench.h" 26#include "eina_bench.h"
27 27
28static void 28static void
29eina_bench_insert_superfast(int request) 29eina_bench_lookup_superfast(int request)
30{ 30{
31 Eina_Hash *hash = NULL; 31 Eina_Hash *hash = NULL;
32 Eina_Array *array = NULL; 32 Eina_Array *array = NULL;
@@ -39,11 +39,11 @@ eina_bench_insert_superfast(int request)
39 39
40 for (i = 0; i < (unsigned int) request; ++i) 40 for (i = 0; i < (unsigned int) request; ++i)
41 { 41 {
42 char *tmp_key = malloc(10); 42 char tmp_key[10];
43 43
44 tmp_val = malloc(sizeof (int)); 44 tmp_val = malloc(sizeof (int));
45 45
46 if (!tmp_key || !tmp_val) continue ; 46 if (!tmp_val) continue ;
47 47
48 snprintf(tmp_key, 10, "%i", i); 48 snprintf(tmp_key, 10, "%i", i);
49 *tmp_val = i; 49 *tmp_val = i;
@@ -51,46 +51,17 @@ eina_bench_insert_superfast(int request)
51 eina_hash_add(hash, tmp_key, tmp_val); 51 eina_hash_add(hash, tmp_key, tmp_val);
52 52
53 eina_array_append(array, tmp_val); 53 eina_array_append(array, tmp_val);
54
55 free(tmp_key);
56 } 54 }
57 55
58 eina_hash_free(hash); 56 srand(time(NULL));
59
60 EINA_ARRAY_ITER_NEXT(array, i, tmp_val)
61 free(tmp_val);
62
63 eina_array_free(array);
64}
65
66static void
67eina_bench_insert_djb2(int request)
68{
69 Eina_Hash *hash = NULL;
70 Eina_Array *array = NULL;
71 int *tmp_val;
72 unsigned int i;
73
74 array = eina_array_new(1000);
75
76 hash = eina_hash_string_djb2_new();
77 57
78 for (i = 0; i < (unsigned int) request; ++i) 58 for (i = 0; i < (unsigned int) request; ++i)
79 { 59 {
80 char *tmp_key = malloc(10); 60 char tmp_key[10];
81
82 tmp_val = malloc(sizeof (int));
83
84 if (!tmp_key || !tmp_val) continue ;
85
86 snprintf(tmp_key, 10, "%i", i);
87 *tmp_val = i;
88
89 eina_hash_add(hash, tmp_key, tmp_val);
90 61
91 eina_array_append(array, tmp_val); 62 snprintf(tmp_key, 10, "%i", rand() % request);
92 63
93 free(tmp_key); 64 tmp_val = eina_hash_find(hash, tmp_key);
94 } 65 }
95 66
96 eina_hash_free(hash); 67 eina_hash_free(hash);
@@ -102,7 +73,7 @@ eina_bench_insert_djb2(int request)
102} 73}
103 74
104static void 75static void
105eina_bench_lookup_superfast(int request) 76eina_bench_lookup_djb2(int request)
106{ 77{
107 Eina_Hash *hash = NULL; 78 Eina_Hash *hash = NULL;
108 Eina_Array *array = NULL; 79 Eina_Array *array = NULL;
@@ -111,15 +82,15 @@ eina_bench_lookup_superfast(int request)
111 82
112 array = eina_array_new(1000); 83 array = eina_array_new(1000);
113 84
114 hash = eina_hash_string_superfast_new(); 85 hash = eina_hash_string_djb2_new();
115 86
116 for (i = 0; i < (unsigned int) request; ++i) 87 for (i = 0; i < (unsigned int) request; ++i)
117 { 88 {
118 char tmp_key[10]; 89 char *tmp_key[10];
119 90
120 tmp_val = malloc(sizeof (int)); 91 tmp_val = malloc(sizeof (int));
121 92
122 if (!tmp_val) continue ; 93 if (!tmp_key || !tmp_val) continue ;
123 94
124 snprintf(tmp_key, 10, "%i", i); 95 snprintf(tmp_key, 10, "%i", i);
125 *tmp_val = i; 96 *tmp_val = i;
@@ -149,7 +120,7 @@ eina_bench_lookup_superfast(int request)
149} 120}
150 121
151static void 122static void
152eina_bench_lookup_djb2(int request) 123eina_bench_lookup_djb2_inline(int request)
153{ 124{
154 Eina_Hash *hash = NULL; 125 Eina_Hash *hash = NULL;
155 Eina_Array *array = NULL; 126 Eina_Array *array = NULL;
@@ -162,20 +133,19 @@ eina_bench_lookup_djb2(int request)
162 133
163 for (i = 0; i < (unsigned int) request; ++i) 134 for (i = 0; i < (unsigned int) request; ++i)
164 { 135 {
165 char *tmp_key = malloc(10); 136 char tmp_key[10];
137 int length;
166 138
167 tmp_val = malloc(sizeof (int)); 139 tmp_val = malloc(sizeof (int));
168 140
169 if (!tmp_key || !tmp_val) continue ; 141 if (!tmp_key || !tmp_val) continue ;
170 142
171 snprintf(tmp_key, 10, "%i", i); 143 length = snprintf(tmp_key, 10, "%i", i) + 1;
172 *tmp_val = i; 144 *tmp_val = i;
173 145
174 eina_hash_add(hash, tmp_key, tmp_val); 146 eina_hash_add_by_hash(hash, tmp_key, length, eina_hash_djb2(tmp_key, length), tmp_val);
175 147
176 eina_array_append(array, tmp_val); 148 eina_array_append(array, tmp_val);
177
178 free(tmp_key);
179 } 149 }
180 150
181 srand(time(NULL)); 151 srand(time(NULL));
@@ -183,10 +153,11 @@ eina_bench_lookup_djb2(int request)
183 for (i = 0; i < (unsigned int) request; ++i) 153 for (i = 0; i < (unsigned int) request; ++i)
184 { 154 {
185 char tmp_key[10]; 155 char tmp_key[10];
156 int length;
186 157
187 snprintf(tmp_key, 10, "%i", rand() % request); 158 length = snprintf(tmp_key, 10, "%i", rand() % request) + 1;
188 159
189 tmp_val = eina_hash_find(hash, tmp_key); 160 tmp_val = eina_hash_find_by_hash(hash, tmp_key, length, eina_hash_djb2(tmp_key, length));
190 } 161 }
191 162
192 eina_hash_free(hash); 163 eina_hash_free(hash);
@@ -199,8 +170,7 @@ eina_bench_lookup_djb2(int request)
199 170
200void eina_bench_hash(Eina_Bench *bench) 171void eina_bench_hash(Eina_Bench *bench)
201{ 172{
202 eina_bench_register(bench, "superfast-insert", EINA_BENCH(eina_bench_insert_superfast), 1000, 180000, 2500);
203 eina_bench_register(bench, "djb2-insert", EINA_BENCH(eina_bench_insert_djb2), 1000, 180000, 2500);
204 eina_bench_register(bench, "superfast-lookup", EINA_BENCH(eina_bench_lookup_superfast), 1000, 180000, 2500); 173 eina_bench_register(bench, "superfast-lookup", EINA_BENCH(eina_bench_lookup_superfast), 1000, 180000, 2500);
205 eina_bench_register(bench, "djb2-lookup", EINA_BENCH(eina_bench_lookup_djb2), 1000, 180000, 2500); 174 eina_bench_register(bench, "djb2-lookup", EINA_BENCH(eina_bench_lookup_djb2), 1000, 180000, 2500);
175 eina_bench_register(bench, "djb2-lookup-inline", EINA_BENCH(eina_bench_lookup_djb2_inline), 1000, 180000, 2500);
206} 176}