/magick.mod/src/magick/timer.c

https://github.com/BlitzMaxModules/bah.mod · C · 415 lines · 141 code · 3 blank · 271 comment · 30 complexity · 831688c6394abf8e6de751e532f79c8a MD5 · raw file

  1. /*
  2. % Copyright (C) 2003 GraphicsMagick Group
  3. % Copyright (C) 2002 ImageMagick Studio
  4. %
  5. % This program is covered by multiple licenses, which are described in
  6. % Copyright.txt. You should have received a copy of Copyright.txt with this
  7. % package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
  8. %
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. % %
  11. % %
  12. % %
  13. % TTTTT IIIII M M EEEEE RRRR %
  14. % T I MM MM E R R %
  15. % T I M M M EEE RRRR %
  16. % T I M M E R R %
  17. % T IIIII M M EEEEE R R %
  18. % %
  19. % %
  20. % GraphicsMagick Timing Methods %
  21. % %
  22. % %
  23. % Software Design %
  24. % John Cristy %
  25. % January 1993 %
  26. % %
  27. % %
  28. % %
  29. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  30. %
  31. % Contributed by Bill Radcliffe and Bob Friesenhahn.
  32. %
  33. */
  34. /*
  35. Include declarations.
  36. */
  37. #include "magick/studio.h"
  38. /*
  39. Define declarations.
  40. */
  41. #if !defined(CLK_TCK)
  42. #define CLK_TCK sysconf(_SC_CLK_TCK)
  43. #endif
  44. /*
  45. Forward declarations.
  46. */
  47. static double
  48. UserTime(void);
  49. static void
  50. StartTimer(TimerInfo *,const unsigned int),
  51. StopTimer(TimerInfo *);
  52. /*
  53. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  54. % %
  55. % %
  56. % %
  57. % C o n t i n u e T i m e r %
  58. % %
  59. % %
  60. % %
  61. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  62. %
  63. % Method ContinueTimer resumes a stopped stopwatch. The stopwatch continues
  64. % counting from the last StartTimer() onwards.
  65. %
  66. % The format of the ContinueTimer method is:
  67. %
  68. % unsigned int ContinueTimer(TimerInfo *time_info)
  69. %
  70. % A description of each parameter follows.
  71. %
  72. % o time_info: Time statistics structure.
  73. %
  74. */
  75. MagickExport unsigned int ContinueTimer(TimerInfo *time_info)
  76. {
  77. assert(time_info != (TimerInfo *) NULL);
  78. assert(time_info->signature == MagickSignature);
  79. if (time_info->state == UndefinedTimerState)
  80. return(False);
  81. if (time_info->state == StoppedTimerState)
  82. {
  83. time_info->user.total-=time_info->user.stop-time_info->user.start;
  84. time_info->elapsed.total-=
  85. time_info->elapsed.stop-time_info->elapsed.start;
  86. }
  87. time_info->state=RunningTimerState;
  88. return(True);
  89. }
  90. /*
  91. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  92. % %
  93. % %
  94. % %
  95. + E l a p s e d T i m e %
  96. % %
  97. % %
  98. % %
  99. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  100. %
  101. % Method ElapsedTime returns the elapsed time (in seconds) since the last
  102. % call to StartTimer().
  103. %
  104. % The format of the ElapsedTime method is:
  105. %
  106. % double ElapsedTime()
  107. %
  108. */
  109. static double ElapsedTime(void)
  110. {
  111. #if defined(HAVE_TIMES)
  112. struct tms
  113. timer;
  114. return((double) times(&timer)/CLK_TCK);
  115. #else
  116. #if defined(MSWINDOWS)
  117. return(NTElapsedTime());
  118. #else
  119. return((double) clock()/CLK_TCK);
  120. #endif
  121. #endif
  122. }
  123. /*
  124. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  125. % %
  126. % %
  127. % %
  128. % G e t E l a p s e d T i m e %
  129. % %
  130. % %
  131. % %
  132. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  133. %
  134. % Method GetElapsedTime returns the elapsed time (in seconds) passed between
  135. % the start and stop events. If the stopwatch is still running, it is stopped
  136. % first.
  137. %
  138. % The format of the GetElapsedTime method is:
  139. %
  140. % double GetElapsedTime(TimerInfo *time_info)
  141. %
  142. % A description of each parameter follows.
  143. %
  144. % o time_info: Timer statistics structure.
  145. %
  146. */
  147. MagickExport double GetElapsedTime(TimerInfo *time_info)
  148. {
  149. assert(time_info != (TimerInfo *) NULL);
  150. assert(time_info->signature == MagickSignature);
  151. if (time_info->state == UndefinedTimerState)
  152. return(0.0);
  153. if (time_info->state == RunningTimerState)
  154. StopTimer(time_info);
  155. return(time_info->elapsed.total);
  156. }
  157. /*
  158. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  159. % %
  160. % %
  161. % %
  162. % G e t T i m e r I n f o %
  163. % %
  164. % %
  165. % %
  166. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  167. %
  168. % Method GetTimerInfo initializes the TimerInfo structure.
  169. %
  170. % The format of the GetTimerInfo method is:
  171. %
  172. % void GetTimerInfo(TimerInfo *time_info)
  173. %
  174. % A description of each parameter follows.
  175. %
  176. % o time_info: Timer statistics structure.
  177. %
  178. */
  179. MagickExport void GetTimerInfo(TimerInfo *time_info)
  180. {
  181. /*
  182. Create a stopwatch and start it.
  183. */
  184. assert(time_info != (TimerInfo *) NULL);
  185. (void) memset(time_info,0,sizeof(TimerInfo));
  186. time_info->state=UndefinedTimerState;
  187. time_info->signature=MagickSignature;
  188. StartTimer(time_info,True);
  189. }
  190. /*
  191. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  192. % %
  193. % %
  194. % %
  195. % G e t T i m e r R e s o l u t i o n %
  196. % %
  197. % %
  198. % %
  199. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  200. %
  201. % Obtain the measurement resolution of the timer.
  202. %
  203. % The format of the GetTimerResolution method is:
  204. %
  205. % void GetTimerInfo(TimerInfo *time_info)
  206. %
  207. % A description of each parameter follows.
  208. %
  209. % o time_info: Timer statistics structure.
  210. %
  211. */
  212. MagickExport double GetTimerResolution(void)
  213. {
  214. #if defined(MSWINDOWS)
  215. return (0.02);
  216. #else
  217. return (1.0/CLK_TCK);
  218. #endif
  219. }
  220. /*
  221. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  222. % %
  223. % %
  224. % %
  225. % G e t U s e r T i m e %
  226. % %
  227. % %
  228. % %
  229. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  230. %
  231. % Method GetUserTime returns the User time (user and system) by the operating
  232. % system (in seconds) between the start and stop events. If the stopwatch is
  233. % still running, it is stopped first.
  234. %
  235. % The format of the GetUserTime method is:
  236. %
  237. % double GetUserTime(TimerInfo *time_info)
  238. %
  239. % A description of each parameter follows.
  240. %
  241. % o time_info: Timer statistics structure.
  242. %
  243. */
  244. MagickExport double GetUserTime(TimerInfo *time_info)
  245. {
  246. assert(time_info != (TimerInfo *) NULL);
  247. assert(time_info->signature == MagickSignature);
  248. if (time_info->state == UndefinedTimerState)
  249. return(0.0);
  250. if (time_info->state == RunningTimerState)
  251. StopTimer(time_info);
  252. return(time_info->user.total);
  253. }
  254. /*
  255. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  256. % %
  257. % %
  258. % %
  259. % R e s e t T i m e r %
  260. % %
  261. % %
  262. % %
  263. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  264. %
  265. % Method ResetTimer resets the stopwatch.
  266. %
  267. % The format of the ResetTimer method is:
  268. %
  269. % void ResetTimer(TimerInfo *time_info)
  270. %
  271. % A description of each parameter follows.
  272. %
  273. % o time_info: Timer statistics structure.
  274. %
  275. */
  276. MagickExport void ResetTimer(TimerInfo *time_info)
  277. {
  278. assert(time_info != (TimerInfo *) NULL);
  279. assert(time_info->signature == MagickSignature);
  280. StopTimer(time_info);
  281. time_info->elapsed.stop=0.0;
  282. time_info->user.stop=0.0;
  283. }
  284. /*
  285. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  286. % %
  287. % %
  288. % %
  289. + S t a r t T i m e r %
  290. % %
  291. % %
  292. % %
  293. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  294. %
  295. % Method StartTimer starts the stopwatch.
  296. %
  297. % The format of the StartTimer method is:
  298. %
  299. % void StartTimer(TimerInfo *time_info,const unsigned int reset)
  300. %
  301. % A description of each parameter follows.
  302. %
  303. % o time_info: Timer statistics structure.
  304. %
  305. % o reset: If reset is True, then the stopwatch is reset prior to starting.
  306. % If reset is False, then timing is continued without resetting the
  307. % stopwatch.
  308. %
  309. */
  310. static void StartTimer(TimerInfo *time_info,const unsigned int reset)
  311. {
  312. assert(time_info != (TimerInfo *) NULL);
  313. assert(time_info->signature == MagickSignature);
  314. if (reset)
  315. {
  316. /*
  317. Reset the stopwatch before starting it.
  318. */
  319. time_info->user.total=0.0;
  320. time_info->elapsed.total=0.0;
  321. }
  322. if (time_info->state != RunningTimerState)
  323. {
  324. time_info->elapsed.start=ElapsedTime();
  325. time_info->user.start=UserTime();
  326. }
  327. time_info->state=RunningTimerState;
  328. }
  329. /*
  330. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  331. % %
  332. % %
  333. % %
  334. + S t o p T i m e r %
  335. % %
  336. % %
  337. % %
  338. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  339. %
  340. % Method StopTimer stops the stopwatch.
  341. %
  342. % The format of the StopTimer method is:
  343. %
  344. % void StopTimer(TimerInfo *time_info)
  345. %
  346. % A description of each parameter follows.
  347. %
  348. % o time_info: Timer statistics structure.
  349. %
  350. */
  351. static void StopTimer(TimerInfo *time_info)
  352. {
  353. assert(time_info != (TimerInfo *) NULL);
  354. assert(time_info->signature == MagickSignature);
  355. time_info->elapsed.stop=ElapsedTime();
  356. time_info->user.stop=UserTime();
  357. if (time_info->state == RunningTimerState)
  358. {
  359. time_info->user.total+=
  360. time_info->user.stop-time_info->user.start+MagickEpsilon;
  361. time_info->elapsed.total+=
  362. time_info->elapsed.stop-time_info->elapsed.start+MagickEpsilon;
  363. }
  364. time_info->state=StoppedTimerState;
  365. }
  366. /*
  367. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  368. % %
  369. % %
  370. % %
  371. + U s e r T i m e %
  372. % %
  373. % %
  374. % %
  375. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  376. %
  377. % Method UserTime returns the total time the process has been scheduled (in
  378. % seconds) since the last call to StartTimer().
  379. %
  380. % The format of the UserTime method is:
  381. %
  382. % double UserTime()
  383. %
  384. */
  385. static double UserTime(void)
  386. {
  387. #if defined(HAVE_TIMES)
  388. struct tms
  389. timer;
  390. (void) times(&timer);
  391. return((double) (timer.tms_utime+timer.tms_stime)/CLK_TCK);
  392. #else
  393. #if defined(MSWINDOWS)
  394. return(NTUserTime());
  395. #else
  396. return((double) clock()/CLK_TCK);
  397. #endif
  398. #endif
  399. }