/core/vul/vul_timer.cxx

https://bitbucket.org/jtotz/vxl · C++ · 199 lines · 136 code · 24 blank · 39 comment · 13 complexity · 422cbbe9f61821fbbb9c4cca40d94721 MD5 · raw file

  1. // This is core/vul/vul_timer.cxx
  2. #include "vul_timer.h"
  3. //:
  4. // \file
  5. //
  6. // Copyright (C) 1991 Texas Instruments Incorporated.
  7. //
  8. // Permission is granted to any individual or institution to use, copy, modify,
  9. // and distribute this software, provided that this complete copyright and
  10. // permission notice is maintained, intact, in all copies and supporting
  11. // documentation.
  12. //
  13. // Texas Instruments Incorporated provides this software "as is" without
  14. // express or implied warranty.
  15. //
  16. // Created: BMK 07/14/89 Initial design and implementation
  17. // Updated: LGO 09/23/89 Conform to COOL coding style
  18. // Updated: AFM 12/31/89 OS/2 port
  19. // Updated: DLS 03/22/91 New lite version
  20. // Updated: VDN 10/14/93 ANSI C does not have user/system time.
  21. //
  22. // The Timer class provides timing code for performance evaluation. This code
  23. // was originally written by Joe Rahmeh at UT Austin.
  24. //
  25. // User time:
  26. // time cpu spends in user mode on behalf of the program.
  27. // System time:
  28. // time cpu spends in system mode on behalf of the program.
  29. // Real time:
  30. // what you get from a stop watch timer.
  31. //
  32. #include <vcl_ctime.h>
  33. #if defined(como4301) && defined(__linux__)
  34. # include <sys/types.h>
  35. # include <sys/select.h>
  36. # define __USE_BSD
  37. #endif
  38. #include <vcl_sys/time.h>
  39. # undef __USE_BSD
  40. struct vul_timer_data
  41. {
  42. #if !defined(VCL_WIN32) || defined(__CYGWIN__)
  43. tms usage0; // usage mark.
  44. struct timeval real0; // wall clock mark.
  45. #else
  46. vcl_clock_t usage0;
  47. # if defined(VCL_BORLAND)
  48. struct timeb real0;
  49. # else
  50. struct _timeb real0;
  51. # endif
  52. #endif
  53. };
  54. #include <vxl_config.h> // VXL_TWO_ARG_GETTIME
  55. #include <vcl_climits.h> // for CLK_TCK
  56. #include <vcl_iostream.h>
  57. //#define CLK_TCK _sysconf(3) in <limits.h> has error
  58. #if defined(VCL_WIN32) && !defined(__CYGWIN__)
  59. #include <direct.h> // for sysconf()
  60. #else
  61. #include <unistd.h>
  62. #endif
  63. #undef CLK_TCK
  64. #define CLK_TCK sysconf(_SC_CLK_TCK)
  65. vul_timer::vul_timer()
  66. : data(new vul_timer_data)
  67. {
  68. mark();
  69. }
  70. vul_timer::~vul_timer()
  71. {
  72. delete data;
  73. data = 0;
  74. }
  75. //: Sets the reference time to now.
  76. void vul_timer::mark()
  77. {
  78. #if !defined(VCL_WIN32) || defined(__CYGWIN__)
  79. times(&data->usage0); // user/system time
  80. #ifndef SYSV
  81. struct timezone tz;
  82. gettimeofday(&data->real0, &tz); // wall clock time
  83. #else
  84. #if VXL_TWO_ARG_GETTIME
  85. gettimeofday(&data->real0, (struct timezone*)0);
  86. #else
  87. gettimeofday(&data->real0);
  88. #endif
  89. #endif
  90. #else
  91. // Win32 section
  92. data->usage0 = vcl_clock();
  93. # if defined(VCL_BORLAND)
  94. ftime(&data->real0);
  95. # else
  96. _ftime(&data->real0);
  97. # endif
  98. #endif
  99. }
  100. //: Returns the number of milliseconds of wall clock time, since last mark().
  101. long vul_timer::real()
  102. {
  103. long s;
  104. #if !defined(VCL_WIN32) || defined(__CYGWIN__)
  105. struct timeval real_time; // new real time
  106. #ifndef SYSV
  107. struct timezone tz;
  108. gettimeofday(&real_time, &tz); // wall clock time
  109. #else
  110. #if VXL_TWO_ARG_GETTIME
  111. gettimeofday(&real_time, (struct timezone*)0);
  112. #else
  113. gettimeofday(&real_time);
  114. #endif
  115. #endif
  116. s = real_time.tv_sec - data->real0.tv_sec;
  117. long us = real_time.tv_usec - data->real0.tv_usec;
  118. if (us < 0) { us += 1000000; --s; }
  119. return long(1000.0*s + us / 1000.0 + 0.5);
  120. #else
  121. // Win32 section
  122. # if defined(VCL_BORLAND)
  123. struct timeb real_time;
  124. ftime(&real_time);
  125. # else
  126. struct _timeb real_time;
  127. _ftime(&real_time);
  128. # endif
  129. s = long(real_time.time - data->real0.time);
  130. long ms = real_time.millitm - data->real0.millitm;
  131. if (ms < 0) { ms += 1000; --s; }
  132. return 1000*s + ms;
  133. #endif
  134. }
  135. long vul_timer::user()
  136. {
  137. #if !defined(VCL_WIN32) || defined(__CYGWIN__)
  138. tms usage;
  139. times(&usage); // new user/system time
  140. return (usage.tms_utime - data->usage0.tms_utime) * 1000 / CLK_TCK;
  141. #else
  142. vcl_clock_t usage = vcl_clock();
  143. return (usage - data->usage0) / (CLOCKS_PER_SEC/1000);
  144. #endif
  145. }
  146. //: Returns the number of milliseconds spent in user-process or operating system respectively, since last mark().
  147. long vul_timer::system()
  148. {
  149. #if !defined(VCL_WIN32) || defined(__CYGWIN__)
  150. tms usage;
  151. times(&usage); // new user/system time
  152. return (usage.tms_stime - data->usage0.tms_stime) * 1000 / CLK_TCK;
  153. #else
  154. return 0L;
  155. #endif
  156. }
  157. // Returns the number of milliseconds spent in user-process AND
  158. // operating system, since last mark().
  159. long vul_timer::all()
  160. {
  161. #if !defined(VCL_WIN32) || defined(__CYGWIN__)
  162. tms usage;
  163. times(&usage); // new user/system time
  164. return (usage.tms_utime + usage.tms_stime -
  165. data->usage0.tms_utime - data->usage0.tms_stime) * 1000 / CLK_TCK;
  166. #else
  167. vcl_clock_t usage = vcl_clock();
  168. return (usage - data->usage0) / (CLOCKS_PER_SEC/1000);
  169. #endif
  170. }
  171. //: Display user and real time since the last mark.
  172. void vul_timer::print(vcl_ostream& s)
  173. {
  174. s << "Time: user " << user() / 1000.0 << ", real " << this->real() / 1000.0 << vcl_endl;
  175. }