forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_libdict.c
99 lines (87 loc) · 2.44 KB
/
test_libdict.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*****************************************************************************
* Copyright (C) 2014-2015
* file: test_libdict.c
* author: gozfree <[email protected]>
* created: 2015-04-29 00:45
* updated: 2015-07-13 03:30
*****************************************************************************/
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "libdict.h"
#define ALIGN "%15s: %6.4f sec\n"
#define NKEYS 1024*1024
//#define NKEYS 10
double epoch_double()
{
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec + (t.tv_usec * 1.0) / 1000000.0;
}
int main(int argc, char * argv[])
{
dict * d;
double t1, t2;
int i;
int nkeys;
char * buffer;
char * val;
nkeys = (argc>1) ? (int)atoi(argv[1]) : NKEYS;
printf("%15s: %d\n", "values", nkeys);
buffer = (char *)malloc(9 * nkeys);
if (!buffer) {
printf("malloc failed!\n");
return -1;
}
d = dict_new();
t1 = epoch_double();
for (i = 0; i < nkeys; i++) {
sprintf(buffer + i * 9, "%08x", i);
}
t2 = epoch_double();
printf(ALIGN, "initialization", t2 - t1);
t1 = epoch_double();
for(i = 0; i < nkeys; i++) {
dict_add(d, buffer + i*9, buffer +i*9);
//printf("hash_set: key=%p, val=%p\n", buffer + i*9, buffer + i*9);
}
//dict_dump(d, stdout);
t2 = epoch_double();
printf(ALIGN, "adding", t2 - t1);
t1 = epoch_double();
for(i = 0; i < nkeys; i++) {
val = dict_get(d, buffer + i*9, (char *)"UNDEF");
if (0) {
printf("hash_get: key=%p, val=%p\n", buffer + i*9, val);
}
#if DEBUG>1
printf("exp[%s] got[%s]\n", buffer+i*9, val);
if (val && strcmp(val, buffer+i*9)) {
printf("-> WRONG got[%s] exp[%s]\n", val, buffer+i*9);
}
#endif
}
t2 = epoch_double();
printf(ALIGN, "lookup", t2 - t1);
// if (nkeys<100)
// dict_dump(d, stdout);
t1 = epoch_double();
for(i = 0; i < nkeys; i++) {
//printf("dict_del %d, string = %s\n", i, buffer+i*9);
dict_del(d, buffer + i*9);
}
t2 = epoch_double();
printf(ALIGN, "delete", t2 - t1);
t1 = epoch_double();
for(i = 0; i < nkeys; i++) {
dict_add(d, buffer + i*9, buffer +i*9);
}
t2 = epoch_double();
printf(ALIGN, "adding", t2 - t1);
t1 = epoch_double();
dict_free(d);
t2 = epoch_double();
printf(ALIGN, "free", t2 - t1);
free(buffer);
return 0;
}