8 KHASH_SET_INIT_STR(str
)
9 KHASH_MAP_INIT_INT(int, unsigned char)
19 } __attribute__ ((__packed__
)) int_packed_t
;
21 #define hash_eq(a, b) ((a).key == (b).key)
22 #define hash_func(a) ((a).key)
24 KHASH_INIT(iun
, int_unpack_t
, char, 0, hash_func
, hash_eq
)
25 KHASH_INIT(ipk
, int_packed_t
, char, 0, hash_func
, hash_eq
)
27 static int data_size
= 5000000;
28 static unsigned *int_data
;
29 static char **str_data
;
36 printf("--- generating data... ");
37 int_data
= (unsigned*)calloc(data_size
, sizeof(unsigned));
38 str_data
= (char**)calloc(data_size
, sizeof(char*));
39 for (i
= 0; i
< data_size
; ++i
) {
40 int_data
[i
] = (unsigned)(data_size
* ((double)x
/ UINT_MAX
) / 4) * 271828183u;
41 sprintf(buf
, "%x", int_data
[i
]);
42 str_data
[i
] = strdup(buf
);
43 x
= 1664525L * x
+ 1013904223L;
48 void ht_destroy_data()
51 for (i
= 0; i
< data_size
; ++i
) free(str_data
[i
]);
52 free(str_data
); free(int_data
);
58 unsigned *data
= int_data
;
63 for (i
= 0; i
< data_size
; ++i
) {
64 k
= kh_put(int, h
, data
[i
], &ret
);
65 kh_val(h
, k
) = i
&0xff;
66 if (!ret
) kh_del(int, h
, k
);
68 printf("[ht_khash_int] size: %u\n", kh_size(h
));
75 char **data
= str_data
;
80 for (i
= 0; i
< data_size
; ++i
) {
81 k
= kh_put(str
, h
, data
[i
], &ret
);
82 if (!ret
) kh_del(str
, h
, k
);
84 printf("[ht_khash_int] size: %u\n", kh_size(h
));
88 void ht_khash_unpack()
91 unsigned *data
= int_data
;
96 for (i
= 0; i
< data_size
; ++i
) {
98 x
.key
= data
[i
]; x
.val
= i
&0xff;
99 k
= kh_put(iun
, h
, x
, &ret
);
100 if (!ret
) kh_del(iun
, h
, k
);
102 printf("[ht_khash_unpack] size: %u (sizeof=%ld)\n", kh_size(h
), sizeof(int_unpack_t
));
106 void ht_khash_packed()
109 unsigned *data
= int_data
;
114 for (i
= 0; i
< data_size
; ++i
) {
116 x
.key
= data
[i
]; x
.val
= i
&0xff;
117 k
= kh_put(ipk
, h
, x
, &ret
);
118 if (!ret
) kh_del(ipk
, h
, k
);
120 printf("[ht_khash_packed] size: %u (sizeof=%ld)\n", kh_size(h
), sizeof(int_packed_t
));
124 void ht_timing(void (*f
)(void))
128 printf("[ht_timing] %.3lf sec\n", (double)(clock() - t
) / CLOCKS_PER_SEC
);
131 int main(int argc
, char *argv
[])
133 if (argc
> 1) data_size
= atoi(argv
[1]);
135 ht_timing(ht_khash_int
);
136 ht_timing(ht_khash_str
);
137 ht_timing(ht_khash_unpack
);
138 ht_timing(ht_khash_packed
);