rtimer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /****************************************************************************
  2. *
  3. * MODULE: iostream
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. *
  8. * Iostream is a library that implements streams, external memory
  9. * sorting on streams, and an external memory priority queue on
  10. * streams. These are the fundamental components used in external
  11. * memory algorithms.
  12. * Credits: The library was developed by Laura Toma. The kernel of
  13. * class STREAM is based on the similar class existent in the GPL TPIE
  14. * project developed at Duke University. The sorting and priority
  15. * queue have been developed by Laura Toma based on communications
  16. * with Rajiv Wickremesinghe. The library was developed as part of
  17. * porting Terraflow to GRASS in 2001. PEARL upgrades in 2003 by
  18. * Rajiv Wickremesinghe as part of the Terracost project.
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  28. * General Public License for more details. *
  29. * **************************************************************************/
  30. #ifndef RTIMER_H
  31. #define RTIMER_H
  32. #ifdef __MINGW32__
  33. #include <time.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <strings.h>
  37. typedef struct {
  38. time_t tv1, tv2;
  39. } Rtimer;
  40. #define rt_start(rt) \
  41. if((time(&(rt.tv1)) == ((time_t) -1))) { \
  42. perror("time"); \
  43. exit(1); \
  44. }
  45. /* doesn't really stop, just updates endtimes */
  46. #define rt_stop(rt) \
  47. if((time(&(rt.tv2)) == ((time_t) -1))) { \
  48. perror("time"); \
  49. exit(1); \
  50. }
  51. #define rt_u_useconds(rt) rt_w_useconds(rt)
  52. #define rt_s_useconds(rt) rt_w_useconds(rt)
  53. #define rt_w_useconds(rt) (1.0e6 * (rt.tv2 - rt.tv1))
  54. #else /* __MINGW32__ */
  55. #include <sys/time.h>
  56. #include <sys/resource.h>
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <strings.h>
  60. typedef struct {
  61. struct rusage rut1, rut2;
  62. struct timeval tv1, tv2;
  63. } Rtimer;
  64. #define rt_start(rt) \
  65. if((getrusage(RUSAGE_SELF, &rt.rut1) < 0) \
  66. || (gettimeofday(&(rt.tv1), NULL) < 0)) { \
  67. perror("rusage/gettimeofday"); \
  68. exit(1); \
  69. }
  70. /* doesn't really stop, just updates endtimes */
  71. #define rt_stop(rt) \
  72. if((getrusage(RUSAGE_SELF, &rt.rut2) < 0) \
  73. || (gettimeofday(&(rt.tv2), NULL) < 0)) { \
  74. perror("rusage/gettimeofday"); \
  75. exit(1); \
  76. }
  77. #define rt_u_useconds(rt) \
  78. (((double)rt.rut2.ru_utime.tv_usec + \
  79. (double)rt.rut2.ru_utime.tv_sec*1000000) \
  80. - ((double)rt.rut1.ru_utime.tv_usec + \
  81. (double)rt.rut1.ru_utime.tv_sec*1000000))
  82. #define rt_s_useconds(rt) \
  83. (((double)rt.rut2.ru_stime.tv_usec + \
  84. (double)rt.rut2.ru_stime.tv_sec*1000000) \
  85. - ((double)rt.rut1.ru_stime.tv_usec + \
  86. (double)rt.rut1.ru_stime.tv_sec*1000000))
  87. #define rt_w_useconds(rt) \
  88. (((double)rt.tv2.tv_usec + \
  89. (double)rt.tv2.tv_sec*1000000) \
  90. - ((double)rt.tv1.tv_usec + \
  91. (double)rt.tv1.tv_sec*1000000))
  92. #endif /* __MINGW32__ */
  93. /* not required to be called, but makes values print as 0.
  94. obviously a hack */
  95. #define rt_zero(rt) bzero(&(rt),sizeof(Rtimer));
  96. #define rt_seconds(rt) (rt_w_useconds(rt)/1000000)
  97. #define rt_sprint(buf, rt) rt_sprint_safe(buf,rt)
  98. char * rt_sprint_safe(char *buf, Rtimer rt);
  99. #endif /* RTIMER_H */