profile.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /* profile.c
  21. *
  22. * used for testing/profiling different code
  23. *
  24. */
  25. #ifdef DEBUG
  26. #include "common.h"
  27. #include "hash_table.h"
  28. double gettime(clock_t start)
  29. {
  30. clock_t end;
  31. double cpu_time_used;
  32. end = clock();
  33. cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
  34. return cpu_time_used;
  35. }
  36. static int my_walk(const void *key, void *dataptr, void *param)
  37. {
  38. char *data = *(char **)dataptr;
  39. printf("key: %s data: %s\n", (char *) key, data);
  40. return 0;
  41. }
  42. #define display_results(_start, _name, _iterations) do { \
  43. total = gettime(start); \
  44. printf("%s: %d iterations; total: %.12fs; avg: %.12fs\n", _name, _iterations, total, total / _iterations); \
  45. } while (0)
  46. void profile(int argc, char **argv)
  47. {
  48. hash_table_t *ht = NULL;
  49. clock_t start;
  50. double total;
  51. start = clock();
  52. ht = hash_table_create(NULL, NULL, 100, HASH_TABLE_STRINGS);
  53. printf("Hash table created with %d rows\n", ht->max_rows);
  54. hash_table_insert(ht, "key1", (void *) "data1");
  55. hash_table_insert(ht, "key2", (void *) "data2");
  56. hash_table_insert(ht, "key3", (void *) "data3");
  57. hash_table_insert(ht, "key4", (void *) "data4");
  58. hash_table_insert(ht, "key5", (void *) "data5");
  59. hash_table_insert(ht, "key6", (void *) "data6");
  60. printf("%d%%\n", ht->cells_in_use / ht->max_rows);
  61. hash_table_walk(ht, my_walk, NULL);
  62. display_results(start, "hash table", 6);
  63. exit(0);
  64. }
  65. #endif /* DEBUG */