speed3.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. ** Written by David Gerdes US Army Construction Engineering Research Lab
  3. ** April 1992
  4. ** Copyright 1992 USA-CERL All rights reserved.
  5. **
  6. */
  7. /*
  8. ** This is a simple performance comparison between linkm and malloc
  9. ** I think it better simulates normal use of the library than
  10. ** speed.c or speed2.c
  11. */
  12. #include <stdio.h>
  13. #include <grass/linkm.h>
  14. struct link
  15. {
  16. char let;
  17. struct link *next;
  18. };
  19. #define LINKM
  20. int main(int argc, char *argv[])
  21. {
  22. register int i, j;
  23. VOID_T *head;
  24. struct link List, *tmp, *p;
  25. #ifdef LINKM
  26. head = (VOID_T *) link_init(sizeof(struct link));
  27. #endif
  28. for (j = 0; j < 1000; j++) {
  29. tmp = &List;
  30. for (i = 0; i < 2000; i++) {
  31. #ifdef LINKM
  32. p = (struct link *)link_new(head);
  33. #else
  34. p = (struct link *)malloc(sizeof(struct link));
  35. #endif
  36. tmp->next = p;
  37. tmp = p;
  38. tmp->next = NULL;
  39. }
  40. for (p = List.next; p != NULL;) {
  41. tmp = p->next;
  42. #ifdef LINKM
  43. link_dispose(head, p);
  44. #else
  45. free(p);
  46. #endif
  47. p = tmp;
  48. }
  49. }
  50. #ifdef LINKM
  51. link_cleanup(head);
  52. #endif
  53. exit(0);
  54. }