forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_libhash.c
95 lines (82 loc) · 2.33 KB
/
test_libhash.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
/******************************************************************************
* Copyright (C) 2014-2015
* file: test_libhash.c
* author: gozfree <[email protected]>
* created: 2015-04-29 00:45
* updated: 2015-04-29 00:45
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include "libhash.h"
#define PALIGN "%15s: %6.4f\n"
#define NKEYS 1024*1024
//#define NKEYS 10240
static double epoch_double(void)
{
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec + (t.tv_usec * 1.0) / 1000000.0;
}
int main(int argc, char * argv[])
{
struct hash * 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);
d = hash_create(2097152);
t1 = epoch_double();
for(i = 0; i < nkeys; i++) {
sprintf(buffer + i * 9, "%08x", i);
}
t2 = epoch_double();
printf(PALIGN, "initialization", t2 - t1);
t1 = epoch_double();
for(i = 0; i < nkeys; i++) {
hash_set(d, buffer + i*9, buffer +i*9);
//printf("hash_set: key=%p, val=%p\n", buffer + i*9, buffer + i*9);
}
t2 = epoch_double();
printf(PALIGN, "adding", t2 - t1);
t1 = epoch_double();
for(i = 0; i < nkeys; i++) {
val = (char *)hash_get(d, buffer + i*9);
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(PALIGN, "lookup", t2 - t1);
// if (nkeys<100)
//dict_dump(d, stdout);
t1 = epoch_double();
for(i = 0; i < nkeys; i++) {
hash_del(d, buffer + i*9);
}
t2 = epoch_double();
printf(PALIGN, "delete", t2 - t1);
t1 = epoch_double();
for(i = 0; i < nkeys; i++) {
hash_set(d, buffer + i*9, buffer +i*9);
}
t2 = epoch_double();
printf(PALIGN, "adding", t2 - t1);
t1 = epoch_double();
hash_destroy(d);
t2 = epoch_double();
printf(PALIGN, "free", t2 - t1);
free(buffer);
return 0 ;
}