profile.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* profile.c
  2. *
  3. * used for testing/profiling different code
  4. *
  5. */
  6. #ifdef DEBUG
  7. #include "common.h"
  8. #include "hash_table.h"
  9. double gettime(clock_t start)
  10. {
  11. clock_t end;
  12. double cpu_time_used;
  13. end = clock();
  14. cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
  15. return cpu_time_used;
  16. }
  17. static int my_walk(const void *key, void *dataptr, void *param)
  18. {
  19. char *data = *(char **)dataptr;
  20. printf("key: %s data: %s\n", (char *) key, data);
  21. return 0;
  22. }
  23. #define display_results(_start, _name, _iterations) do { \
  24. total = gettime(start); \
  25. printf("%s: %d iterations; total: %.12fs; avg: %.12fs\n", _name, _iterations, total, total / _iterations); \
  26. } while (0)
  27. void profile(int argc, char **argv)
  28. {
  29. hash_table_t *ht = NULL;
  30. clock_t start;
  31. double total;
  32. start = clock();
  33. ht = hash_table_create(NULL, NULL, 100, HASH_TABLE_STRINGS);
  34. printf("Hash table created with %d rows\n", ht->max_rows);
  35. hash_table_insert(ht, "key1", (void *) "data1");
  36. hash_table_insert(ht, "key2", (void *) "data2");
  37. hash_table_insert(ht, "key3", (void *) "data3");
  38. hash_table_insert(ht, "key4", (void *) "data4");
  39. hash_table_insert(ht, "key5", (void *) "data5");
  40. hash_table_insert(ht, "key6", (void *) "data6");
  41. printf("%d%%\n", ht->cells_in_use / ht->max_rows);
  42. hash_table_walk(ht, my_walk, NULL);
  43. display_results(start, "hash table", 6);
  44. exit(0);
  45. }
  46. #endif /* DEBUG */