Diff for /libaitio/example/Attic/rb.c between versions 1.1 and 1.2

version 1.1, 2011/06/07 11:22:04 version 1.2, 2011/06/07 11:49:39
Line 0 Line 1
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #include <unistd.h>
   #include <atree.h>
   #include <sys/time.h>
   
   struct blah {
           int n;
           RB_ENTRY(blah) node;
           SPLAY_ENTRY(blah) node1;
           AVL_ENTRY(blah) node2;
   };
   RB_HEAD(rb_head, blah) rbh;
   SPLAY_HEAD(sp_head, blah) sph;
   AVL_HEAD(avl_head, blah) avlh;
   
   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, 0);
           srandom(getpid());
   
           gettimeofday(&before, NULL);
           for (i = 0; i < 1000000; i++) {
                   b = malloc(sizeof(struct blah));
                   memset(b, 0, sizeof(struct blah));
                   b->n = random();
                   if (RB_INSERT(rb_head, &rbh, b))
                           free(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);
           for (i = 0; i < 1000000; i++) {
                   b = malloc(sizeof(struct blah));
                   memset(b, 0, sizeof(struct blah));
                   b->n = random();
                   if (SPLAY_INSERT(sp_head, &sph, b))
                           free(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);
           for (i = 0; i < 1000000; i++) {
                   b = malloc(sizeof(struct blah));
                   memset(b, 0, sizeof(struct blah));
                   b->n = random();
                   if (AVL_INSERT(avl_head, &avlh, b))
                           free(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 < 10000; i++) {
                   memset(b2, 0, sizeof(struct blah));
                   b2->n = random();
                   b = SPLAY_FIND(sp_head, &sph, b2);
                   if (b)
                           printf("SP::Found key %d(%s) -> %d\n", i, RB_COLOR(b, node) == RB_BLACK ? "black" : "red", 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 < 10000; i++) {
                   memset(b2, 0, sizeof(struct blah));
                   b2->n = random();
                   b = RB_FIND(rb_head, &rbh, b2);
                   if (b)
                           printf("RB::Found key %d(%s) -> %d\n", i, RB_COLOR(b, node) == RB_BLACK ? "black" : "red", b->n);
           }
           gettimeofday(&after, NULL);
           printf("RB::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6);
           free(b2);
   
           printf("---\n");
   
           b2 = malloc(sizeof(struct blah));
           gettimeofday(&before, NULL);
           for (i = 0; i < 10000; 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-> %d next=%d prev=%d\n", i, b2->n, b->n, 
                                           AVL_NEXT(avl_head, &avlh, b) ? AVL_NEXT(avl_head, &avlh, b)->n : 0,
                                           AVL_PREV(avl_head, &avlh, b) ? AVL_PREV(avl_head, &avlh, b)->n : 0);
           }
           gettimeofday(&after, NULL);
           printf("AVL::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6);
   
           printf("---\n");
   
           gettimeofday(&before, NULL);
           AVL_FOREACH_SAFE(b, avl_head, &avlh, b2) {
                   /*
                   printf("aaa %p[%d] %p[%d] --- ", b, b ? b->n : 0, b2, b2 ? b2->n : 0);
                   printf("b:%p %p /// ", b ? AVL_LEFT(b, node2) : b, b ? AVL_RIGHT(b, node2) : b);
                   printf("b2:%p %p\n", b2 ? AVL_LEFT(b2, node2) : b2, b2 ? AVL_RIGHT(b2, node2) : b2); fflush(stdout);
                   */
                   AVL_REMOVE(avl_head, &avlh, b);
                   free(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);
           for (b = SPLAY_MIN(sp_head, &sph); b; b = b2) {
                   b2 = SPLAY_NEXT(sp_head, &sph, b);
                   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);
                   free(b);
           }
           gettimeofday(&after, NULL);
           printf("RB::Time %f\n", (after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1e6);
           return !AVL_EMPTY(&avlh);
   }

Removed from v.1.1  
changed lines
  Added in v.1.2


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>