home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / clock.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  6KB  |  243 lines

  1. /***
  2. *clock.c - Contains the clock runtime
  3. *
  4. *       Copyright (c) 1987-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       The clock runtime returns the processor time used by
  8. *       the current process.
  9. *
  10. *******************************************************************************/
  11.  
  12. #ifdef _WIN32
  13.  
  14.  
  15. #include <cruntime.h>
  16. #include <windows.h>
  17. #include <stdio.h>
  18. #include <time.h>
  19.  
  20. #include <internal.h>
  21. #include <sys\timeb.h>
  22. #include <sys\types.h>
  23.  
  24.  
  25.  
  26. void __cdecl __inittime(void);
  27.  
  28. #ifdef _MSC_VER
  29.  
  30. #pragma data_seg(".CRT$XIC")
  31. static _PVFV pinit = __inittime;
  32.  
  33. #pragma data_seg()
  34.  
  35. #endif  /* _MSC_VER */
  36.  
  37. static unsigned __int64 start_tics;
  38.  
  39. /***
  40. *clock_t clock() - Return the processor time used by this process.
  41. *
  42. *Purpose:
  43. *       This routine calculates how much time the calling process
  44. *       has used.  At startup time, startup calls __inittime which stores
  45. *       the initial time.  The clock routine calculates the difference
  46. *       between the current time and the initial time.
  47. *
  48. *       Clock must reference _cinitime so that _cinitim.asm gets linked in.
  49. *       That routine, in turn, puts __inittime in the startup initialization
  50. *       routine table.
  51. *
  52. *Entry:
  53. *       No parameters.
  54. *       itime is a static structure of type timeb.
  55. *
  56. *Exit:
  57. *       If successful, clock returns the number of CLK_TCKs (milliseconds)
  58. *       that have elapsed.  If unsuccessful, clock returns -1.
  59. *
  60. *Exceptions:
  61. *       None.
  62. *
  63. *******************************************************************************/
  64.  
  65. clock_t __cdecl clock (
  66.         void
  67.         )
  68. {
  69.         unsigned __int64 current_tics;
  70.         FILETIME ct;
  71.  
  72.         GetSystemTimeAsFileTime( &ct );
  73.  
  74.         current_tics = (unsigned __int64)ct.dwLowDateTime +
  75.                        (((unsigned __int64)ct.dwHighDateTime) << 32);
  76.  
  77.         /* calculate the elapsed number of 100 nanosecond units */
  78.         current_tics -= start_tics;
  79.  
  80.         /* return number of elapsed milliseconds */
  81.         return (clock_t)(current_tics / 10000);
  82. }
  83.  
  84. /***
  85. *void __inittime() - Initialize the time location
  86. *
  87. *Purpose:
  88. *       This routine stores the time of the process startup.
  89. *       It is only linked in if the user issues a clock runtime call.
  90. *
  91. *Entry:
  92. *       No arguments.
  93. *
  94. *Exit:
  95. *       No return value.
  96. *
  97. *Exceptions:
  98. *       None.
  99. *
  100. *******************************************************************************/
  101.  
  102. void __cdecl __inittime (
  103.         void
  104.         )
  105. {
  106.         FILETIME st;
  107.  
  108.         GetSystemTimeAsFileTime( &st );
  109.  
  110.         start_tics = (unsigned __int64)st.dwLowDateTime +
  111.                      (((unsigned __int64)st.dwHighDateTime) << 32);
  112. }
  113.  
  114.  
  115.  
  116. #else  /* _WIN32 */
  117.  
  118. #if defined (_M_MPPC) || defined (_M_M68K)
  119.  
  120.  
  121. #include <cruntime.h>
  122. #include <stdio.h>
  123. #include <time.h>
  124. #include <fltintrn.h>            /* PFV definition */
  125. #include <sys\timeb.h>
  126. #include <sys\types.h>
  127. #include <internal.h>
  128.  
  129. /* define the entry in initializer table */
  130.  
  131. #pragma data_seg(".CRT$XIC")
  132.  
  133. static PFV __pinittime = _inittime;
  134.  
  135. #pragma data_seg()
  136.  
  137. static struct _timeb __itimeb;
  138.  
  139. /***
  140. *clock_t clock() - Return the processor time used by this process.
  141. *
  142. *Purpose:
  143. *       This routine calculates how much time the calling process
  144. *       has used.  At startup time, startup calls _inittime which stores
  145. *       the initial time.  The clock routine calculates the difference
  146. *       between the current time and the initial time.
  147. *
  148. *       Clock must reference _cinitime so that _cinitim.asm gets linked in.
  149. *       That routine, in turn, puts _inittime in the startup initialization
  150. *       routine table.
  151. *
  152. *Entry:
  153. *       No parameters.
  154. *       itime is a static structure of type timeb.
  155. *
  156. *Exit:
  157. *       If successful, clock returns the number of CLK_TCKs (milliseconds)
  158. *       that have elapsed.  If unsuccessful, clock returns -1.
  159. *
  160. *Exceptions:
  161. *       None.
  162. *
  163. *******************************************************************************/
  164.  
  165. clock_t __cdecl clock (
  166.         void
  167.         )
  168. {
  169.         struct _timeb now;
  170.         clock_t elapsed;
  171.  
  172.         /* Calculate the difference between the initial time and now. */
  173.  
  174.         _ftime(&now);
  175.         elapsed = (now.time - __itimeb.time) * CLOCKS_PER_SEC;
  176.         elapsed += (int)now.millitm - (int)__itimeb.millitm;
  177.         return(elapsed);
  178.  
  179. }
  180.  
  181.  
  182.  
  183. /***
  184. *void _inittime() - Initialize the time location
  185. *
  186. *Purpose:
  187. *       This routine stores the process's time of startup.
  188. *       It is only linked in if the user issues a clock runtime
  189. *       call.
  190. *
  191. *Entry:
  192. *       No arguments.
  193. *
  194. *Exit:
  195. *       No return value.
  196. *
  197. *Exceptions:
  198. *       None.
  199. *
  200. *******************************************************************************/
  201.  
  202. void __cdecl _inittime (
  203.         void
  204.         )
  205. {
  206.         _ftime(&__itimeb);
  207. }
  208.  
  209.  
  210. /***
  211. *time_t _GetApplicationStartTime() - Initialize the time location
  212. *
  213. *Purpose:
  214. *       This routine stores the process's time of startup.
  215. *       It is only linked in if the user issues a clock runtime
  216. *       call.
  217. *
  218. *Entry:
  219. *       No arguments.
  220. *
  221. *Exit:
  222. *       initial application start time.
  223. *
  224. *Exceptions:
  225. *       None.
  226. *
  227. *******************************************************************************/
  228.  
  229. time_t __cdecl _GetApplicationStartTime (
  230.         void
  231.         )
  232. {
  233.  
  234.         /* The reference to _tzset causes _inittime to be executed at startup time.*/
  235.         _tzset();
  236.         return __itimeb.time ;
  237. }
  238.  
  239.  
  240. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  241.  
  242. #endif  /* _WIN32 */
  243.