]> git.kaiwu.me - klib.git/commitdiff
test performance given packed struct
authorHeng Li <lh3@me.com>
Thu, 2 May 2013 19:51:14 +0000 (15:51 -0400)
committerHeng Li <lh3@me.com>
Thu, 2 May 2013 19:51:14 +0000 (15:51 -0400)
test/khash_test.c

index 8e5416382daf836112bda3c7534d94da49ccb43c..3a4167067c153830018de5287ba6fa3c605e3b27 100644 (file)
@@ -8,6 +8,22 @@
 KHASH_SET_INIT_STR(str)
 KHASH_SET_INIT_INT(int)
 
+typedef struct {
+       unsigned key;
+       unsigned char val;
+} int_unpack_t;
+
+typedef struct {
+       unsigned key;
+       unsigned char val;
+} __attribute__ ((__packed__)) int_packed_t;
+
+#define hash_eq(a, b) ((a).key == (b).key)
+#define hash_func(a) ((a).key)
+
+KHASH_INIT(iun, int_unpack_t, char, 0, hash_func, hash_eq)
+KHASH_INIT(ipk, int_packed_t, char, 0, hash_func, hash_eq)
+
 static int data_size = 5000000;
 static unsigned *int_data;
 static char **str_data;
@@ -28,12 +44,14 @@ void ht_init_data()
        }
        printf("done!\n");
 }
+
 void ht_destroy_data()
 {
        int i;
        for (i = 0; i < data_size; ++i) free(str_data[i]);
        free(str_data); free(int_data);
 }
+
 void ht_khash_int()
 {
        int i, ret;
@@ -49,6 +67,7 @@ void ht_khash_int()
        printf("[ht_khash_int] size: %u\n", kh_size(h));
        kh_destroy(int, h);
 }
+
 void ht_khash_str()
 {
        int i, ret;
@@ -64,18 +83,58 @@ void ht_khash_str()
        printf("[ht_khash_int] size: %u\n", kh_size(h));
        kh_destroy(str, h);
 }
+
+void ht_khash_unpack()
+{
+       int i, ret;
+       unsigned *data = int_data;
+       khash_t(iun) *h;
+       unsigned k;
+
+       h = kh_init(iun);
+       for (i = 0; i < data_size; ++i) {
+               int_unpack_t x;
+               x.key = data[i]; x.val = i&0xff;
+               k = kh_put(iun, h, x, &ret);
+               if (!ret) kh_del(iun, h, k);
+       }
+       printf("[ht_khash_unpack] size: %u (sizeof=%ld)\n", kh_size(h), sizeof(int_unpack_t));
+       kh_destroy(iun, h);
+}
+
+void ht_khash_packed()
+{
+       int i, ret;
+       unsigned *data = int_data;
+       khash_t(ipk) *h;
+       unsigned k;
+
+       h = kh_init(ipk);
+       for (i = 0; i < data_size; ++i) {
+               int_packed_t x;
+               x.key = data[i]; x.val = i&0xff;
+               k = kh_put(ipk, h, x, &ret);
+               if (!ret) kh_del(ipk, h, k);
+       }
+       printf("[ht_khash_packed] size: %u (sizeof=%ld)\n", kh_size(h), sizeof(int_packed_t));
+       kh_destroy(ipk, h);
+}
+
 void ht_timing(void (*f)(void))
 {
        clock_t t = clock();
        (*f)();
        printf("[ht_timing] %.3lf sec\n", (double)(clock() - t) / CLOCKS_PER_SEC);
 }
+
 int main(int argc, char *argv[])
 {
        if (argc > 1) data_size = atoi(argv[1]);
        ht_init_data();
        ht_timing(ht_khash_int);
        ht_timing(ht_khash_str);
+       ht_timing(ht_khash_unpack);
+       ht_timing(ht_khash_packed);
        ht_destroy_data();
        return 0;
 }