home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / euphor10.zip / DATABASE.C < prev    next >
C/C++ Source or Header  |  1993-05-28  |  4KB  |  162 lines

  1. /* C version of database update benchmark */
  2.  
  3. #define ITERATIONS 50000
  4.  
  5. #include <stdio.h>
  6. #include <time.h>
  7.  
  8. char *realloc();
  9. char *malloc();
  10.  
  11. /* update commands */
  12. #define NEW     1   /* add a new account */
  13. #define    UPDATE  2   /* add/substract from their account */
  14. #define    DELETE  3   /* delete someone */
  15.  
  16. struct rd {
  17.     char *name;
  18.     double amount;
  19.     int code;
  20. } raw_data[] = {
  21. {"George Bush", 1000, NEW},
  22. {"Bill Clinton", 2000, NEW},
  23. {"Brian Mulroney", 500, NEW},
  24. {"Ross Perot", 10000, NEW},
  25. {"Ross Perot", 0, DELETE},
  26. {"George Bush", -30.55, UPDATE},
  27. {"Madonna", 2500, NEW},
  28. {"Boris Yeltsin", 100, NEW},
  29. {"Michael Jackson", 50, NEW},
  30. {"Peter Mansbridge", 1200, NEW},
  31. {"Bill Clinton", +500, UPDATE},
  32. {"Rod Stewart", 3000, NEW},
  33. {"Boris Yeltsin", 0, DELETE},
  34. {"Sharon Stone", 1500, NEW},
  35. {"Clint Eastwood", 1900, NEW},
  36. {"Madonna", 0, DELETE },
  37. {"Sally Jessy Raphael", 750, NEW},
  38. {"Brian Mulroney", -400, DELETE},
  39. {"Richard Gere", 299, NEW},
  40. {"Rod Stewart", 0, DELETE},
  41. {"Demi Moore", 350, NEW},
  42. {"Bruce Willis", 480, NEW},
  43. {"Sharon Stone", +900.50, UPDATE},
  44. {"Arsenio Hall", 300, NEW},
  45. {"David Letterman", 450, NEW},
  46. {"Whoopi Goldberg", 1050, NEW},
  47. {"Clint Eastwood", +2500, UPDATE},
  48. {"Michael Jackson", -50, UPDATE},
  49. {"Clint Eastwood", 0, DELETE},
  50. {"Jack Nicholson", 3000, NEW}
  51. };
  52.  
  53. /* The database */
  54. struct db_rec {
  55.     char *name;         /* person's name */
  56.     double amount;    /* account balance */
  57. } *database = NULL;
  58.  
  59. int size = 0;        /* current database size */
  60.  
  61. dump()
  62. /* used to verify that program works correctly
  63.    not part of timing loop */
  64. {
  65.     int i;
  66.  
  67.     for (i = 0; i < size; i++) {
  68.     printf("%20s: %8.2f\n", database[i].name, database[i].amount);
  69.     }
  70. }
  71.  
  72. purge()
  73. /* empty the database - free all storage */
  74. {
  75.     int i;
  76.  
  77.     if (database != NULL) {
  78.     for (i = 0; i < size; i++)
  79.         free(database[i].name);
  80.         free(database);
  81.     }
  82.     size = 0;
  83.     database = NULL;
  84. }
  85.  
  86. Out_Of_Space()
  87. {
  88.     fprintf(stderr, "out of space\n");
  89.     exit(1);
  90. }
  91.  
  92. update(struct rd *data_stream, int nitems)
  93. {
  94.     int i, action, account_no;
  95.     char *name_copy;
  96.     struct db_rec *new_database;
  97.     int j, k;
  98.     char *pname;
  99.     struct rd *record;
  100.  
  101.     for (i = 0; i < nitems; i++) {
  102.     record = &data_stream[i];
  103.     action = record->code;
  104.     
  105.     if (action == NEW) {
  106.         size++;
  107.         database = (struct db_rec *)realloc(database, 
  108.                         size * sizeof(struct db_rec));
  109.         if (database == NULL) 
  110.         Out_Of_Space();
  111.         name_copy = (char *)malloc(strlen(record->name)+1);
  112.         if (name_copy == NULL)
  113.         Out_Of_Space();
  114.         strcpy(name_copy, record->name);
  115.         database[size-1].name = name_copy;
  116.         database[size-1].amount = record->amount;
  117.     }
  118.     else {
  119.         pname = data_stream[i].name;
  120.         for (account_no = 0; account_no < size; account_no++) {
  121.         if (strcmp(pname, database[account_no].name) == 0)
  122.             break;
  123.         }
  124.         if (action == UPDATE) {
  125.             database[account_no].amount += record->amount;
  126.         }
  127.          else if (action == DELETE) {
  128.             size--;
  129.             new_database = (struct db_rec *)malloc(size * 
  130.                                sizeof(struct db_rec));
  131.             if (new_database == NULL)
  132.             Out_Of_Space();
  133.             j = 0;
  134.             for (k = 0; k < account_no; k++) {
  135.             new_database[j] = database[k];
  136.             j++;
  137.             }
  138.             for (k = account_no+1; k <= size; k++) {
  139.             new_database[j] = database[k];
  140.             j++;
  141.             }
  142.             free(database);
  143.             database = new_database;
  144.         }
  145.         }
  146.     }        
  147. }
  148.  
  149. main()
  150. {
  151.     long i, t;
  152.  
  153.     t = clock();
  154.     for (i = 1; i <= ITERATIONS; i++) {
  155.         purge();
  156.         update(&raw_data, sizeof(raw_data)/sizeof(struct rd));
  157.     }
  158.     printf("%d iterations in %.2f seconds\n", ITERATIONS, 
  159.        (clock() - t)/(double)CLOCKS_PER_SEC);
  160.     dump();
  161. }
  162.