original_timer.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  5. <meta name="GENERATOR" content="Microsoft FrontPage 5.0">
  6. <meta name="ProgId" content="FrontPage.Editor.Document">
  7. <title>Original Timers</title>
  8. <style type="text/css">
  9. ins {background-color:#A0FFA0}
  10. del {background-color:#FFA0A0}
  11. body
  12. {
  13. font-family: sans-serif;
  14. max-width : 8.5in;
  15. margin: 1em;
  16. }
  17. </style>
  18. </head>
  19. <body bgcolor="#FFFFFF" text="#000000">
  20. <table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="750">
  21. <tr>
  22. <td width="300">
  23. <a href="../../../index.htm">
  24. <img src="../../../boost.png" alt="boost.png (6897 bytes)" align="middle" width="300" height="86" border="0"></a></td>
  25. <td align="middle" width="430">
  26. <font size="7">Timer Library<br>
  27. Original Timers and Progress Display</font></td>
  28. </tr>
  29. </table>
  30. <table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF" width="100%">
  31. <tr>
  32. <td><a href="index.html">Timer Home</a> &nbsp;&nbsp;
  33. <a href="cpu_timers.html">CPU timers</a> &nbsp;&nbsp;
  34. <a href="original_timer.html">Original timers</a> &nbsp;&nbsp;
  35. </td>
  36. </tr>
  37. </table>
  38. <p></p>
  39. <center>
  40. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="80%">
  41. <tr>
  42. <td width="100%" bgcolor="#FFFF66">
  43. <p><i><b>These timers are deprecated.</b></i> They date from the earliest days
  44. of Boost and do not conform
  45. to current Boost practice.</p>
  46. <ul>
  47. <li>The interfaces are the same across all platforms, but the semantics differ
  48. according to platform. Wall-clock time is measured on Windows, while CPU time
  49. is measured on POSIX-like systems.</li>
  50. <li>The internal implementation uses only C/C++ standard library functions, so
  51. cannot supply desirable precision and functionality.</li>
  52. <li>The headers live in the main Boost header directory.</li>
  53. <li>The content are in namespace <code>boost</code>.</li>
  54. </ul>
  55. Please see the Version 2
  56. <a href="cpu_timers.html">CPU timers</a> for replacements that conform to
  57. current Boost practice.</td>
  58. </tr>
  59. </table>
  60. </center>
  61. <p>The timer library provides two headers and three classes: </p>
  62. <blockquote>
  63. <table border="1" cellpadding="5">
  64. <tr>
  65. <td><b>Header</b></td>
  66. <td><b>Class</b></td>
  67. <td><b>Functionality</b></td>
  68. </tr>
  69. <tr>
  70. <td><a href="../../../boost/timer.hpp">timer.hpp</a></td>
  71. <td><a href="#Class timer">timer</a></td>
  72. <td>Measure elapsed time.</td>
  73. </tr>
  74. <tr>
  75. <td><a href="../../../boost/progress.hpp">progress.hpp</a></td>
  76. <td><a href="#Class progress_timer">progress_timer</a></td>
  77. <td>Measure elapsed time (using timer), display on destruction.</td>
  78. </tr>
  79. <tr>
  80. <td><a href="../../../boost/progress.hpp">progress.hpp</a></td>
  81. <td><a href="#Class progress_display">progress_display</a></td>
  82. <td>Display an indication of progress toward a known goal.</td>
  83. </tr>
  84. </table>
  85. </blockquote>
  86. <p>The objective in designing these classes was fairly limited - they are
  87. intended for simple uses like timing and reporting progress for programmer's
  88. tests or batch job streams. The specifications of the progress classes are
  89. worded in very general terms to permit alternate implementations such as for
  90. graphical user interfaces.</p>
  91. <h2><a name="Class timer">Class timer</a></h2>
  92. <p>Class timer measures elapsed time.&nbsp; It is generally useful for minor
  93. timing tasks.&nbsp; Its supplied implementation offers moderate portability at
  94. the cost of depending on the unknown accuracy and precision of the C Standard
  95. Library clock() function.&nbsp; The maximum measurable elapsed time may be as
  96. low as 596.5 hours (or even less) for the supplied implementation. Because of
  97. these limitations, this timer cannot be depended upon to
  98. be robust, and should not be used if that is of any concern.</p>
  99. <h3>Synopsis</h3>
  100. <pre>#include &lt;<a href="../../../boost/timer.hpp">boost/timer.hpp</a>&gt;
  101. namespace boost {
  102. class timer {
  103. public:
  104. timer(); // postcondition: elapsed()==0
  105. // compiler generated copy constructor, copy assignment, and dtor apply
  106. void restart(); // post: elapsed()==0
  107. double elapsed() const; // return elapsed time in seconds
  108. double elapsed_max() const; // return estimated maximum value for elapsed()
  109. // Portability warning: elapsed_max() may return too high a value on systems
  110. // where std::clock_t overflows or resets at surprising values.
  111. double elapsed_min() const; // return minimum value for elapsed()
  112. }; // timer
  113. } // namespace boost</pre>
  114. <h3>Exception safety</h3>
  115. <p>The constructors may throw <code>std::bad_alloc</code>.&nbsp; No other member
  116. functions throw exceptions.</p>
  117. <h3>Future directions</h3>
  118. <p>There was a very reasonable request from Ed Brey for a method of determining
  119. the maximum value which may be returned by elapsed(), but there isn't a way to do so
  120. portably. The issue has been raised with the group working on extended time functionality for the C language. A solution
  121. may be years in the future. In the meantime, elapsed_max() provides an
  122. approximation.</p>
  123. <h2><a name="Class progress_timer">Class progress_timer</a></h2>
  124. <p>Class progress_timer automatically measures elapsed time, and then on
  125. destruction displays an elapsed time message at an appropriate place in an appropriate form.&nbsp;
  126. The supplied implementation defaults to a character display on std::cout.</p>
  127. <p>Class progress_timer is often used to time program execution.&nbsp; Its use is as simple as:</p>
  128. <blockquote>
  129. <pre>#include &lt;<a href="../../../boost/progress.hpp">boost/progress.hpp</a>&gt;
  130. int main()
  131. {
  132. progress_timer t; // start timing
  133. // do something ...
  134. return 0;
  135. }</pre>
  136. </blockquote>
  137. <p>Which will produce some appropriate output, for example:</p>
  138. <blockquote>
  139. <pre>1.23 s</pre>
  140. </blockquote>
  141. <p>Note that &quot;s&quot; is the official System International d'Unités
  142. abbreviation for seconds.</p>
  143. <h3>Synopsis</h3>
  144. <pre>#include &lt;<a href="../../../boost/progress.hpp">boost/progress.hpp</a>&gt;
  145. namespace boost {
  146. class progress_timer : public <a href="#Class timer">timer</a>, <a href="../../core/doc/html/core/noncopyable.html">noncopyable</a> {
  147. public:
  148. progress_timer();
  149. progress_timer( std::ostream&amp; os ); // os is hint; implementation may ignore
  150. ~progress_timer();
  151. }; // progress_display
  152. } // namespace boost</pre>
  153. <h3>Exception safety</h3>
  154. <p>The constructors may throw <code>std::bad_alloc</code>.&nbsp; No other member
  155. functions throw exceptions.</p>
  156. <h2><a name="Class progress_display">Class progress_display</a></h2>
  157. <p>Class progress_display displays an appropriate indication of progress toward
  158. a predefined goal at an appropriate place in an appropriate form.&nbsp; This
  159. meets a human need to know if a program is progressing.</p>
  160. <p>For example, if a lengthy computation must be done on a std::map&lt;&gt;
  161. named big_map, the follow code would display an indication of progress:</p>
  162. <pre> progress_display show_progress( big_map.size() );
  163. for ( big_map_t::iterator itr = big_map:begin();
  164. itr != big_map.end(); ++itr )
  165. {
  166. // do the computation
  167. ...
  168. ++show_progress;
  169. }</pre>
  170. <p>After 70% of the elements have been processed, the display might look
  171. something like this:</p>
  172. <blockquote>
  173. <pre>0% 10 20 30 40 50 60 70 80 90 100%
  174. |----|----|----|----|----|----|----|----|----|----|
  175. ************************************</pre>
  176. </blockquote>
  177. <h2>Synopsis</h2>
  178. <pre>#include &lt;boost/progress.hpp&gt;
  179. namespace boost {
  180. class progress_display : <a href="../../core/doc/html/core/noncopyable.html">noncopyable</a> {
  181. public:
  182. progress_display( unsigned long expected_count );
  183. // Effects: restart(expected_count)
  184. progress_display( unsigned long expected_count,
  185. std::ostream&amp; os, // os is hint; implementation may ignore
  186. const std::string &amp; s1 = &quot;\n&quot;, //leading strings
  187. const std::string &amp; s2 = &quot;&quot;,
  188. const std::string &amp; s3 = &quot;&quot; )
  189. // Effects: save copy of leading strings, restart(expected_count)
  190. void restart( unsigned long expected_count );
  191. // Effects: display appropriate scale on three lines,
  192. // prefaced by stored copy of s1, s2, s3, respectively, from constructor
  193. // Postconditions: count()==0, expected_count()==expected_count
  194. unsigned long operator+=( unsigned long increment )
  195. // Effects: Display appropriate progress tic if needed.
  196. // Postconditions: count()== original count() + increment
  197. // Returns: count().
  198. unsigned long operator++()
  199. // Returns: operator+=( 1 ).
  200. unsigned long count() const
  201. // Returns: The internal count.
  202. unsigned long expected_count() const
  203. // Returns: The expected_count from the constructor.
  204. }; // progress_display
  205. } // namespace boost</pre>
  206. <h3>Exception safety</h3>
  207. <p>All member functions except count() and expected_count() do output, and so in
  208. theory may throw exceptions.&nbsp; In practice it seems an exception being
  209. thrown is pretty unlikely, and probably implies such serious problems that an
  210. exception is warranted.&nbsp; Note that there is no explicit destructor, so the
  211. destructor throwing is not an issue.</p>
  212. <h2>History</h2>
  213. <p>These classes are descended from older C++ and C functionality found useful
  214. by programmers for many years. Via the Boost mailing list, Reid Sweatman
  215. suggested separating the more widely useful timer class from the more targeted
  216. progress classes. Sean Corfield suggested allowing output to any ostream.&nbsp;
  217. Dave Abrahams, Valentin Bonnard, Ed Brey, Andy Glew, and Dietmar Kühl also
  218. provided useful comments.&nbsp; Ed Brey suggested timer::elapsed_max(). John
  219. Maddock suggested timer::elapsed_min(). Toon Knapen suggested the optional
  220. leading strings, to allow for labeling the progress display</p>
  221. <h2>Rationale</h2>
  222. <p>The early versions of the timer classes had separate implementation
  223. files.&nbsp; This caused problems for users not wishing to build libraries,
  224. caused difficulties building DLL's (because of cascaded use of other libraries
  225. which in turn brought illuminated compiler deficiencies), and caused the classes
  226. not to be used even when clearly applicable.&nbsp; Thus the implementation was
  227. changed to all inline code.</p>
  228. <p>There have been several requests for platform specific implementations to use
  229. supposedly high-performance timers from the operating system API.&nbsp; John
  230. Maddock submitted an implementation using the Win32 API.&nbsp; Tests showed that
  231. while the precision of these timers was high, the latency was sometimes very
  232. much higher than for the std::clock() function, and that is very bad.&nbsp;
  233. Furthermore, results using the Win32 API were very dependent on both the
  234. compiler (Microsoft and Borland were tested) and the operating system version
  235. (Windows NT, Windows 95, etc.)&nbsp; Thus the std::clock() function was much
  236. more reliable, and so was retained even on this platform with its own timer API.</p>
  237. <hr>
  238. <p><font size="2">Revised:
  239. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B %Y" startspan -->04 October 2011<!--webbot bot="Timestamp" endspan i-checksum="32185" --></font></p>
  240. <p><font size="2">© Copyright Beman Dawes 1999.</font></p>
  241. <p><font size="2">Distributed under the Boost Software License, Version 1.0. See
  242. </font>
  243. <a href="http://www.boost.org/LICENSE_1_0.txt"><font size="2">www.boost.org/LICENSE_1_0.txt</font></a></p>
  244. </body>
  245. </html>