#include #include #include #include #include #include struct blah { int n; RB_ENTRY(blah) node; SPLAY_ENTRY(blah) node1; AVL_ENTRY(blah) node2; SLIST_ENTRY(blah) node3; }; RB_HEAD(rb_head, blah) rbh; SPLAY_HEAD(sp_head, blah) sph; AVL_HEAD(avl_head, blah) avlh; SLIST_HEAD(, blah) slh; static int rb_cmp(struct blah *a, struct blah *b) { int n = (a ? a->n : 0) - (b ? b->n : 0); if (!n) return 0; else if (n < 0) return -1; else return 1; } RB_PROTOTYPE(rb_head, blah, node, rb_cmp); RB_GENERATE(rb_head, blah, node, rb_cmp); SPLAY_PROTOTYPE(sp_head, blah, node1, rb_cmp); SPLAY_GENERATE(sp_head, blah, node1, rb_cmp); AVL_PROTOTYPE(avl_head, blah, node2, rb_cmp) AVL_GENERATE(avl_head, blah, node2, rb_cmp) int main() { struct blah *b, *b2; register int i; struct timeval before, after; RB_INIT(&rbh); SPLAY_INIT(&sph); AVL_INIT(&avlh); SLIST_INIT(&slh); srandom(getpid()); for (i = 0; i < 100000; i++) { b = malloc(sizeof(struct blah)); memset(b, 0, sizeof(struct blah)); b->n = random(); SLIST_INSERT_HEAD(&slh, b, node3); } gettimeofday(&before, NULL); SLIST_FOREACH(b, &slh, node3) RB_INSERT(rb_head, &rbh, b); gettimeofday(&after, NULL); printf("RB::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6); gettimeofday(&before, NULL); SLIST_FOREACH(b, &slh, node3) SPLAY_INSERT(sp_head, &sph, b); gettimeofday(&after, NULL); printf("SP::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6); gettimeofday(&before, NULL); SLIST_FOREACH(b, &slh, node3) AVL_INSERT(avl_head, &avlh, b); gettimeofday(&after, NULL); printf("AVL::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6); printf("---\n"); b2 = malloc(sizeof(struct blah)); gettimeofday(&before, NULL); for (i = 0; i < 20000; i++) { memset(b2, 0, sizeof(struct blah)); b2->n = random(); b = SPLAY_FIND(sp_head, &sph, b2); if (b) printf("SP::Found key %d -> %d\n", i, b->n); } gettimeofday(&after, NULL); printf("SP::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6); printf("---\n"); gettimeofday(&before, NULL); for (i = 0; i < 20000; i++) { memset(b2, 0, sizeof(struct blah)); b2->n = random(); b = RB_FIND(rb_head, &rbh, b2); if (b) printf("RB::Found key %d -> %d\n", i, b->n); } gettimeofday(&after, NULL); printf("RB::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6); printf("---\n"); gettimeofday(&before, NULL); for (i = 0; i < 20000; i++) { memset(b2, 0, sizeof(struct blah)); b2->n = random(); b = AVL_FIND(avl_head, &avlh, b2); if (b) printf("AVL::Found key %d -> %d\n", i, b->n); } gettimeofday(&after, NULL); printf("AVL::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6); printf("---\n"); free(b2); gettimeofday(&before, NULL); AVL_FOREACH_SAFE(b, avl_head, &avlh, b2) AVL_REMOVE(avl_head, &avlh, b); gettimeofday(&after, NULL); printf("AVL::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6); gettimeofday(&before, NULL); SPLAY_FOREACH_SAFE(b, sp_head, &sph, b2) SPLAY_REMOVE(sp_head, &sph, b); gettimeofday(&after, NULL); printf("SP::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6); gettimeofday(&before, NULL); RB_FOREACH_SAFE(b, rb_head, &rbh, b2) RB_REMOVE(rb_head, &rbh, b); gettimeofday(&after, NULL); printf("RB::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6); gettimeofday(&before, NULL); while ((b = SLIST_FIRST(&slh))) { SLIST_REMOVE(&slh, b, blah, node3); free(b); } gettimeofday(&after, NULL); printf("SLIST::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6); return !AVL_EMPTY(&avlh); }