cpu_timers.html 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <meta name="GENERATOR" content="Microsoft FrontPage 5.0">
  5. <meta name="ProgId" content="FrontPage.Editor.Document">
  6. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  7. <title>CPU 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>
  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. CPU Timers</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. <h2><a name="Introduction">Introduction</a></h2>
  39. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" align="right">
  40. <tr>
  41. <td width="100%" bgcolor="#D7EEFF" align="center">
  42. <i><b>Contents</b></i></td>
  43. </tr>
  44. <tr>
  45. <td width="100%" bgcolor="#E8F5FF">
  46. <a href="#Introduction">Introduction</a><br>
  47. <a href="#Example">Using the timers</a><br>
  48. &nbsp; <a href="#using-auto_cpu_timer">Using <code>auto_cpu_timer</code></a><br>
  49. &nbsp; <a href="#using-cpu_timer">Using <code>cpu_timer</code></a><br>
  50. <a href="#Timer-accuracy">Timer accuracy</a><br>
  51. &nbsp; <a href="#Resolution">Resolution</a><br>
  52. &nbsp; <a href="#Other-concerns">Other concerns</a><br>
  53. &nbsp; <a href="#Recommendations">Recommendations</a><br>
  54. <a href="#Reference">Reference</a><br>
  55. <code>&nbsp;<a href="#Synopsis">&lt;boost/timer/timer.hpp&gt;</a></code><a href="#Synopsis">
  56. synopsis</a><br>
  57. &nbsp; <a href="#Default-format">Default format</a><br>
  58. &nbsp; <a href="#nanosecond_type">Typedef <code>nanosecond_type</code></a><br>
  59. &nbsp; <a href="#Namespace-scope-functions">Namespace scope functions</a><br>
  60. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#format"><code>format()</code></a><br>
  61. &nbsp;
  62. <a href="#Class-cpu_timer">Class <code>cpu_timer</code></a><br>
  63. &nbsp;&nbsp;<code>&nbsp; <a href="#cpu_timer-constructors">cpu_timer</a></code><a href="#cpu_timer-constructors">
  64. constructors, destructor</a><br>
  65. &nbsp;&nbsp;&nbsp; &nbsp;<code><a href="#cpu_timer-observers">cpu_timer</a></code><a href="#cpu_timer-observers">
  66. observers</a><br>
  67. &nbsp;&nbsp;<code>&nbsp; <a href="#cpu_timer-actions">cpu_timer</a></code><a href="#cpu_timer-actions">
  68. actions</a><br>
  69. &nbsp; <a href="#Class-auto_cpu_timer">Class <code>auto_cpu_timer</code></a><br>
  70. &nbsp;&nbsp;&nbsp;<code> <a href="#auto_cpu_timer-constructors">auto_cpu_timer</a></code><a href="#auto_cpu_timer-constructors"> constructors</a><br>
  71. &nbsp;&nbsp;&nbsp;<code> <a href="#auto_cpu_timer-destructor">auto_cpu_timer</a></code><a href="#auto_cpu_timer-destructor"> destructor</a><br>
  72. &nbsp;&nbsp;&nbsp;&nbsp; <a href="#auto_cpu_timer-observers"><code>
  73. auto_cpu_timer</code> observers</a><br>
  74. &nbsp;&nbsp;&nbsp;<code> <a href="#auto_cpu_timer-actions">auto_cpu_timer</a></code><a href="#auto_cpu_timer-actions"> actions</a><br>
  75. <a href="#History">History</a><br>
  76. <a href="#Acknowledgements">Acknowledgements</a></tr>
  77. </table>
  78. <p>Knowing how long a program takes to execute is useful in both test and
  79. production environments. It may also be helpful if such timing information is broken down
  80. into wall clock time, CPU time spent by the user, and CPU time spent by the
  81. operating system servicing user requests.</p>
  82. <p>Class <code><a href="#Class-cpu_timer">cpu_timer</a></code> measures
  83. wall clock time, user CPU process time, and system CPU process time. Class <code>
  84. <a href="#Class-auto_cpu_timer">auto_cpu_timer</a></code> is a refinement of
  85. <code>cpu_timer</code> that automatically reports the elapsed times when an <code>
  86. auto_cpu_timer</code> object is destroyed.</p>
  87. <h2><a name="Setup">Setup</a></h2>
  88. <p>Boost.Timer is implemented as a separately compiled library, so you must
  89. install binaries in a location that can be found by your linker. If you followed
  90. the
  91. <a href="http://www.boost.org/doc/libs/release/more/getting_started/index.html">
  92. Boost Getting Started</a> instructions, that's already done for you.</p>
  93. <h2><a name="Example">Using the timers</a></h2>
  94. <h3>Using <code><a name="using-auto_cpu_timer">auto_cpu_timer</a></code></h3>
  95. <p>The simplest and most common use is to add the two lines highlighted below
  96. to a scope you want to time. See <code>
  97. <a href="../example/auto_cpu_timer_example.cpp">auto_cpu_timer_example.cpp</a></code>
  98. for the source code. </p>
  99. <blockquote>
  100. <pre><span style="background-color: #D7EEFF">#include &lt;boost/timer/</span><span style="background-color: #D7EEFF">timer.hpp</span><span style="background-color: #D7EEFF">&gt;</span>
  101. #include &lt;cmath&gt;
  102. int main()
  103. {
  104. <span style="background-color: #D7EEFF">boost::timer::auto_cpu_timer</span><span style="background-color: #D7EEFF"> t;</span>
  105. for (long i = 0; i &lt; 100000000; ++i)
  106. std::sqrt(123.456L); // burn some time
  107. return 0;
  108. }</pre>
  109. </blockquote>
  110. <p>When the <code>auto_cpu_timer</code> object is created, it starts timing. When
  111. it is destroyed at the end of the scope, its destructor stops the timer and
  112. displays timing information on the default output stream, <code>std::cout</code>.</p>
  113. <p>The output of this program will look something like this:</p>
  114. <p><code>&nbsp;&nbsp;&nbsp; 5.713010s wall, 5.709637s user + 0.000000s system =
  115. 5.709637s CPU (99.9%)</code></p>
  116. <p>In other words, this program ran in <code>5.713010</code> seconds as would be measured by a
  117. clock on the wall, the operating system charged it for <code>5.709637</code> seconds of user CPU
  118. time and 0 seconds of system CPU time, the total of these two was <code>5.709637</code>, and that
  119. represented <code>99.9</code> percent of the wall clock time.</p>
  120. <p>The output stream, number of decimal places reported, and reporting format
  121. can be controlled by <code>auto_cpu_timer</code> constructor arguments. Here is
  122. what the output from the above program would look like for several different
  123. sets of constructor arguments:</p>
  124. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
  125. <tr>
  126. <td><i><b>Construction</b></i></td>
  127. <td><i><b>Output</b></i></td>
  128. </tr>
  129. <tr>
  130. <td><code><font size="1">t</font></code></td>
  131. <td><code><font size="1">5.713010s wall, 5.709637s user + 0.000000s system = 5.709637s
  132. CPU (99.9%)</font></code></td>
  133. </tr>
  134. <tr>
  135. <td><code><font size="1">t(std::cerr</font><font size="1">, 2)</font></code></td>
  136. <td><code><font size="1">5.71s wall, 5.70s user + 0.00s system = 5.70s CPU (99.9%)</font></code></td>
  137. </tr>
  138. <tr>
  139. <td><code><font size="1">t(1)</font></code></td>
  140. <td><code><font size="1">5.7s wall, 5.7s user + 0.0s system = 5.7s CPU (99.9%)</font></code></td>
  141. </tr>
  142. <tr>
  143. <td><code><font size="1">t(3, &quot;%w seconds\n&quot;)</font></code></td>
  144. <td><code><font size="1">5.713 seconds<br>
  145. &nbsp;</font></code></td>
  146. </tr>
  147. <tr>
  148. <td><code><font size="1">t(&quot;%t</font><font size="1"> sec CPU, %w sec real&quot;)
  149. </font> </code></td>
  150. <td><code><font size="1">5.709637 sec CPU, 5.713010 sec real</font></code></td>
  151. </tr>
  152. </table>
  153. <p> The processing of the format string is described <a href="#format">here</a>.</p>
  154. <h3> Using <code><a name="using-cpu_timer">cpu_timer</a></code></h3>
  155. <p> The following code creates a checkpoint every 20 CPU seconds:</p>
  156. <blockquote>
  157. <pre>using boost::timer::cpu_timer;
  158. using boost::timer::cpu_times;
  159. using boost::timer::nanosecond_type;
  160. ...
  161. nanosecond_type const twenty_seconds(20 * 1000000000LL);
  162. nanosecond_type last(0);
  163. cpu_timer timer;
  164. while (more_transactions)
  165. {
  166. process_a_transaction();
  167. cpu_times const elapsed_times(timer.elapsed());
  168. nanosecond_type const elapsed(elapsed_times.system
  169. + elapsed_times.user);
  170. if (elapsed &gt;= twenty_seconds)
  171. {
  172. ... create a checkpoint ...
  173. last = elapsed;
  174. }
  175. }</pre>
  176. </blockquote>
  177. <h2><a name="Timer-accuracy">Timer accuracy</a></h2>
  178. <p>How accurate are these timers? </p>
  179. <h3><a name="Resolution">Resolution</a></h3>
  180. <p dir="ltr">The resolution of a clock, and thus timers built on that clock,
  181. is the minimum period time that can be measured. The program <code>
  182. <a href="../test/cpu_timer_info.cpp">cpu_timer_info.cpp</a></code> measures
  183. the resolution of <code>cpu_timer</code>.</p>
  184. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
  185. <tr>
  186. <td rowspan="2" bgcolor="#D7EEFF">O/S</td>
  187. <td rowspan="2" bgcolor="#D7EEFF">Processor</td>
  188. <td colspan="2" align="center" bgcolor="#D7EEFF">Wall-clock</td>
  189. <td colspan="2" align="center" bgcolor="#D7EEFF">CPU</td>
  190. </tr>
  191. <tr>
  192. <td bgcolor="#D7EEFF">Resolution</td>
  193. <td bgcolor="#D7EEFF">Comments</td>
  194. <td align="center" bgcolor="#D7EEFF">User<br>
  195. Resolution</td>
  196. <td align="center" bgcolor="#D7EEFF">System<br>
  197. Resolution</td>
  198. </tr>
  199. <tr>
  200. <td>Mac OS X Lion</td>
  201. <td>Intel circa 2007</td>
  202. <td align="right">2100ns<br>
  203. 2200ns</td>
  204. <td>Some variation within a range.</td>
  205. <td>10000000ns</td>
  206. <td>10000000ns</td>
  207. </tr>
  208. <tr>
  209. <td>Ubuntu Linux 11.4</td>
  210. <td>Intel circa 2005</td>
  211. <td align="right">516ns</td>
  212. <td>Very little variation, typically less than 5ns </td>
  213. <td>10000000ns</td>
  214. <td>10000000ns</td>
  215. </tr>
  216. <tr>
  217. <td>Windows 7</td>
  218. <td>Intel Core i7 860 @ 2.9 GHz</td>
  219. <td align="right">366ns</td>
  220. <td>Some variation, usually in multiples of 366ns</td>
  221. <td>15600100ns</td>
  222. <td>15600100ns</td>
  223. </tr>
  224. <tr>
  225. <td>Windows 7</td>
  226. <td>Intel Mobile T7200 @ 2.0 GHz</td>
  227. <td align="right">2050ns</td>
  228. <td>Much variation. Resolution degrades when processor slows, probably due
  229. to known chipset errata. </td>
  230. <td>15600100ns</td>
  231. <td>15600100ns</td>
  232. </tr>
  233. <tr>
  234. <td>Windows XP</td>
  235. <td>Intel Atom N2800 @ 1.0 GHz</td>
  236. <td align="right">1437ns</td>
  237. <td>Some variation.</td>
  238. <td>15625000ns</td>
  239. <td>15625000ns</td>
  240. </tr>
  241. </table>
  242. <h3><a name="Other-concerns">Other concerns</a></h3>
  243. <p>Wall-clock timings are subject to many outside influences, such as the impact
  244. of other processes.</p>
  245. <blockquote>
  246. <p><code>cpu_timer</code> and <code>auto_cpu_timer</code> obtain Wall-clock
  247. timings from Boost.Chrono's <code>high_resolution_clock</code>. On Intel
  248. compatible CPU's running Windows, Linux, and Mac OS X, this is a &quot;steady
  249. clock&quot; [C++11 20.11.3], but may not be steady on other platforms. <code>
  250. <a href="../test/cpu_timer_info.cpp">cpu_timer_info.cpp</a></code> reports
  251. whether or not the <code>high_resolution_clock</code> is steady on a
  252. particular platform.</p>
  253. <p><i><b><a name="Steady-clocks">Steady clocks</a></b></i> are defined by the
  254. C++11 standard as clocks for which values never decrease as physical time
  255. advances and for which values advance at a steady rate relative to real time.
  256. That is, the clock may not be adjusted. Clocks that are steady never run
  257. backwards, even when the operating system's clock is reset backwards such as
  258. during a daylight saving time transition.</p>
  259. </blockquote>
  260. <p>Timings of debug builds are often several times slower
  261. than release builds, because compiler optimization is turned off and
  262. because libraries often supply very expensive error checks on debug builds.</p>
  263. <p>Synthetic benchmark code may be optimized way, particularly if NDEBUG is
  264. defined. It may be
  265. necessary to inspect generated code to verify this isn't happening.</p>
  266. <h3 dir="ltr"><a name="Recommendations">Recommendations</a></h3>
  267. <p dir="ltr">Think about what is important to your application. For a
  268. production process, the wall clock time may be what is most important. To
  269. study the efficiency of code, total CPU time (user + system) is often a much better measure.</p>
  270. <p dir="ltr">A useful recommendation is to never trust timings unless they are
  271. (1) at least 100 times longer than the CPU time resolution, (2) run multiple
  272. times, and (3) run on release builds. And results that are too good to be true
  273. need to be should be investigated skeptically.</p>
  274. <p>Shared libraries (DLLs and .so's) may incur extra time delays, including expensive
  275. disk accesses, the first time a timer or other function is called. If that
  276. would be misleading, static linking should be considered.</p>
  277. <h2> <a name="Reference">Reference</a></h2>
  278. <p> Specifications are given in the style of the C++ standard library (C++11,
  279. 17.5.1.4 [structure.specifications]). An additional <i>Overview</i> element may
  280. be provided to aid understanding. <i>Overview</i> elements are only informative
  281. - actual semantics are given by the other detailed specification elements.</p>
  282. <p dir="ltr"> Functions not specified as <code>noexcept</code> will throw <code>
  283. std::bad_alloc</code> exceptions if a memory allocation error occurs. Other
  284. errors are reported by time values of -1. [<i>Note:</i> Modern hardware and
  285. operating systems have robust clock subsystems, so such errors are unusual if
  286. even possible at all. <i>-- end note</i>]</p>
  287. <p dir="ltr"> The Timer library meets the same data race avoidance requirements
  288. as the C++11 standard library (17.6.5.9 [res.on.data.races]). Shared objects of
  289. Timer library types risk undefined behavior unless the user supplies a locking
  290. mechanism. See C++11, 17.6.4.10 [res.on.objects], <i>Shared objects and the
  291. library</i>. </p>
  292. <h3>
  293. <code>&lt;boost/timer/timer.hpp&gt;</code> <a name="Synopsis">synopsis</a></h3>
  294. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
  295. <tr>
  296. <td bgcolor="#D7EEFF">
  297. <blockquote>
  298. <pre>namespace boost
  299. {
  300. namespace timer
  301. {
  302. class <a href="#Class-cpu_timer">cpu_timer</a>; // wall clock, user, and system timer
  303. class <a href="#Class-auto_cpu_timer">auto_cpu_timer</a>; // automatic report() on destruction
  304. typedef boost::int_least64_t nanosecond_type;
  305. struct cpu_times
  306. {
  307. nanosecond_type wall;
  308. nanosecond_type user;
  309. nanosecond_type system;
  310. void clear();
  311. };
  312. const int <a name="default_places">default_places</a> = 6;
  313. std::string format(const cpu_times&amp; times, short places, const std::string&amp; format);
  314. std::string format(const cpu_times&amp; times, short places = default_places);
  315. } // namespace timer
  316. } // namespace boost</pre>
  317. </blockquote>
  318. </td>
  319. </tr>
  320. </table>
  321. <h3><a name="Default-format">Default format</a></h3>
  322. <p>The default format is &quot; %ws wall, %us user + %ss system = %ts CPU (%p%)\n&quot;.</p>
  323. <h3>Typedef <a name="nanosecond_type"><code>nanosecond_type</code></a></h3>
  324. <p>The typedef <code>nanosecond_type</code> provides an implementation defined type capable
  325. of representing nanoseconds. For POSIX and Windows systems, <code>
  326. nanoseconds_type</code> is <code>boost::int_least64_t</code>.</p>
  327. <p>The underlying type is not based on the Boost Date-Time or Chrono library to avoid a
  328. dependency on a large library. This design choice may change at some future
  329. date.</p>
  330. <p>Although <code>nanosecond_type</code> is capable of representing one <b>
  331. nanosecond</b>, the actual resolution of common operating system timers may be
  332. much lower. For wall clock time on desktop systems circa 2010, resolution is
  333. often no better than than one <b>microsecond</b>. For user and system time, typical
  334. resolution is 15 <b>milliseconds</b> on Windows and 10 <b>milliseconds</b> on
  335. POSIX.</p>
  336. <h3><a name="cpu_times">Struct <code>cpu_times</code></a></h3>
  337. <p>Struct <code>cpu_times</code> packages the elapsed wall clock time, user
  338. process CPU time, and system process CPU time. See
  339. <a href="#Current-time-values">Current time values</a> for definitions of the
  340. source of these elapsed times.</p>
  341. <pre><span style="background-color: #D7EEFF">void clear();</span></pre>
  342. <blockquote>
  343. <p><i>Effects:</i> <code>wall = user = system = 0LL</code>.</p>
  344. </blockquote>
  345. <h3><a name="Namespace-scope-functions">Namespace scope functions</a></h3>
  346. <pre><span style="background-color: #D7EEFF">std::string </span><a name="format"><span style="background-color: #D7EEFF">format</span></a><span style="background-color: #D7EEFF">(const </span><a href="#cpu_times"><span style="background-color: #D7EEFF">cpu_times</span></a><span style="background-color: #D7EEFF">&amp; times, short places, const std::string&amp; format);
  347. std::string </span><a name="format-2"><span style="background-color: #D7EEFF">format</span></a><span style="background-color: #D7EEFF">(const </span><a href="#cpu_times"><span style="background-color: #D7EEFF">cpu_times</span></a><span style="background-color: #D7EEFF">&amp; times, short places = default_places);</span></pre>
  348. <blockquote>
  349. <p><i>Overview: </i>Converts <code>times</code>'s values to strings representing
  350. seconds to <code>places</code> decimal places, and inserts them into the return
  351. string as controlled by <code>format</code>.</p>
  352. <p><i>Remarks:</i> For the overload without the <code>format</code>
  353. argument, the <a href="#Default-format">default format</a> is used as <code>format</code>.</p>
  354. <p><i>Returns:</i> A string that is a copy of <code>format</code>, except that any
  355. instances of the sequences shown below are replaced by the indicated value.
  356. Times are reported in seconds,
  357. shown to <code>std::max(0, std::min(default_places, 9))</code> decimal places. Percentage is reported to one
  358. decimal place. [<i>Note:</i> percentage may exceed 100% due to differences in
  359. how operating systems measure various times. <i>--end note</i>]</p>
  360. <p><i><b><a name="Format-replacement-sequences">Format replacement sequences</a></b></i></p>
  361. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
  362. <tr>
  363. <td align="center" bgcolor="#D7EEFF"><b><i>Sequence</i></b></td>
  364. <td align="center" bgcolor="#D7EEFF"><b><i>Replacement value</i></b></td>
  365. </tr>
  366. <tr>
  367. <td align="center"><code>%w</code></td>
  368. <td><code>times.wall</code></td>
  369. </tr>
  370. <tr>
  371. <td align="center"><code>%u</code></td>
  372. <td><code>times.user</code></td>
  373. </tr>
  374. <tr>
  375. <td align="center"><code>%s</code></td>
  376. <td><code>times.system</code></td>
  377. </tr>
  378. <tr>
  379. <td align="center"><code>%t</code></td>
  380. <td><code>times.user + times.system</code></td>
  381. </tr>
  382. <tr>
  383. <td align="center"><code>%p</code></td>
  384. <td>The percentage of <code>times.wall</code> represented by <code>
  385. times.user + times.system</code></td>
  386. </tr>
  387. </table>
  388. </blockquote>
  389. <h3><a name="Class-cpu_timer">Class <code>cpu_timer</code></a></h3>
  390. <p> <code>cpu_timer</code> objects measure wall clock elapsed time and process elapsed
  391. time charged to the user and system.</p>
  392. <p><i><b><a name="Current-time-values">Current time values</a></b></i> are the
  393. current wall clock time, user process time, and system process time as provided
  394. by the operating system:</p>
  395. <ul>
  396. <li>Wall clock time is time as would be measured by
  397. an ordinary wristwatch or clock on the wall.</li>
  398. <li>User process time is &quot;the CPU time charged for the
  399. execution of user instructions of the calling process.&quot; See
  400. <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/times.html">
  401. POSIX</a>.</li>
  402. <li>System process time is &quot;the CPU time charged for
  403. execution by the system on behalf of the calling process.&quot; See
  404. <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/times.html">
  405. POSIX</a>.</li>
  406. </ul>
  407. <h3> <a name="cpu_timer-synopsis"> <code>cpu_timer</code> synopsis</a></h3>
  408. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
  409. <tr>
  410. <td bgcolor="#D7EEFF">
  411. <pre>
  412. class <a name="cpu_timer">cpu_timer</a>
  413. {
  414. public:
  415. // constructor
  416. <a href="#cpu_timer-ctor">cpu_timer</a>() noexcept;
  417. // compiler generated; shown for exposition only
  418. ~cpu_timer() noexcept = default;
  419. cpu_timer(const cpu_timer&amp;)&nbsp;noexcept = default;
  420. cpu_timer&amp; operator=(const cpu_timer&amp;) noexcept = default;
  421. // observers
  422. bool <a href="#is_stopped">is_stopped</a>() const noexcept;
  423. cpu_times <a href="#elapsed">elapsed</a>() const noexcept;
  424. std::string <a href="#cpu_timer-format">format</a>(int places, const std::string&amp; format) const;
  425. std::string <a href="#cpu_timer-format">format</a>(int places = default_places) const;
  426. // actions
  427. void <a href="#start">start</a>() noexcept;
  428. void <a href="#stop">stop</a>() noexcept;
  429. void <a href="#resume">resume</a>() noexcept;
  430. };</pre>
  431. </td>
  432. </tr>
  433. </table>
  434. <h3><a name="cpu_timer-constructors"><code>cpu_timer</code> constructor</a></h3>
  435. <pre><span style="background-color: #D7EEFF"><a name="cpu_timer-ctor">cpu_timer</a>() noexcept;</span></pre>
  436. <blockquote>
  437. <p><i>Effects:</i> Constructs an object of type <code>
  438. cpu_timer</code>. Calls<code> start()</code>.</p>
  439. </blockquote>
  440. <h3><a name="cpu_timer-observers"><code>cpu_timer</code>
  441. observers</a></h3>
  442. <pre><span style="background-color: #D7EEFF">bool</span><span style="background-color: #D7EEFF"> <a name="is_stopped">is_stopped</a>() const noexcept;</span></pre>
  443. <blockquote>
  444. <p><i>Returns:</i> <code>true</code> if <a href="#stop">stop()</a> was the most recent
  445. <a href="#cpu_timer-actions">action</a> function called,
  446. otherwise <code>false</code>.</p>
  447. </blockquote>
  448. <pre><span style="background-color: #D7EEFF">cpu_times</span><span style="background-color: #D7EEFF"> <a name="elapsed">elapsed</a>() const noexcept;</span></pre>
  449. <blockquote>
  450. <p><i>Returns:</i> If <code>is_stopped()</code>, the accumulated elapsed times
  451. as of the previous <a href="#stop">stop()</a>. Otherwise, the elapsed times
  452. accumulated between the most recent call to <a href="#start">start()</a> or
  453. <a href="#resume">resume()</a> and the <a href="#Current-time-values">current
  454. time values</a>.</p>
  455. </blockquote>
  456. <pre><span style="background-color: #D7EEFF">std::string </span><a name="cpu_timer-format"><span style="background-color: #D7EEFF">format</span></a><span style="background-color: #D7EEFF">(int</span><span style="background-color: #D7EEFF"> places, const std::string&amp; format) const;
  457. std::string </span><a name="cpu_timer-format-2"><span style="background-color: #D7EEFF">format</span></a><span style="background-color: #D7EEFF">(int</span><span style="background-color: #D7EEFF"> places = </span><span style="background-color: #D7EEFF">default_places</span><span style="background-color: #D7EEFF">) const;</span></pre>
  458. <blockquote>
  459. <p><i>Overview:</i> Returns a string for the current elapsed time as formatted
  460. by the <a href="#format">format non-member function</a>.</p>
  461. <p><i>Returns:</i> <code>boost::<a href="#format">timer::format</a>(<a href="#elapsed">elapsed</a>(), places<i>[, format]</i>)</code>.</p>
  462. </blockquote>
  463. <h3><a name="cpu_timer-actions"><code>cpu_timer</code>
  464. actions</a></h3>
  465. <pre><span style="background-color: #D7EEFF">void <a name="start">start</a>() noexcept;</span></pre>
  466. <blockquote>
  467. <p dir="ltr"><i>Effects:</i> Begins accumulating elapsed time as of the <a href="#Current-time-values">current time values</a>.</p>
  468. <p><i>Postconditions:</i> <code>!is_stopped()</code>.</p>
  469. </blockquote>
  470. <pre><span style="background-color: #D7EEFF">void <a name="stop">stop</a>() noexcept;</span></pre>
  471. <blockquote>
  472. <p><i>Effects:</i> If <code>!is_stopped()</code>, stops accumulating elapsed
  473. time as of the <a href="#Current-time-values">current time values</a>.</p>
  474. <blockquote>
  475. <p>[<i>Note:</i> This is observable via <code>elapsed()</code>. <i>-- end note</i>]</p>
  476. </blockquote>
  477. <p><i>Postconditions:</i> <code>is_stopped()</code>.</p>
  478. </blockquote>
  479. <pre><span style="background-color: #D7EEFF">void <a name="resume">resume</a>() noexcept;</span></pre>
  480. <blockquote>
  481. <p><i>Overview:</i> Restarts the timer, accumulating additional elapsed time.</p>
  482. <p><i>Effects:</i> If <code>is_stopped()</code>, resumes accumulating
  483. additional elapsed time, as of the <a href="#Current-time-values">current time values</a>. Otherwise, no effect.</p>
  484. </blockquote>
  485. <h3><a name="Class-auto_cpu_timer">Class <code>auto_cpu_timer</code></a></h3>
  486. <p>Class <code>auto_cpu_timer</code> adds a <code>report()</code>
  487. function to <code>class cpu_timer</code>, and automatically calls <code>report()</code>
  488. on destruction.</p>
  489. <h3> <a name="auto_cpu_timer-synopsis"> <code>auto_cpu_timer</code> synopsis</a></h3>
  490. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
  491. <tr>
  492. <td bgcolor="#D7EEFF">
  493. <pre>
  494. class <a name="auto_cpu_timer">auto_cpu_timer</a> : public <a href="#cpu_timer">cpu_timer</a>
  495. {
  496. public:
  497. explicit <a href="#auto_cpu_timer-1">auto_cpu_timer</a>(short places = default_places);
  498. <a href="#auto_cpu_timer-2">auto_cpu_timer</a>(short places, const std::string&amp; format);
  499. explicit <a href="#auto_cpu_timer-3">auto_cpu_timer</a>(const std::string&amp; format);
  500. <a href="#auto_cpu_timer-4">auto_cpu_timer</a>(std::ostream&amp; os, short places, const std::string&amp; format);
  501. explicit <a href="#auto_cpu_timer-5">auto_cpu_timer</a>(std::ostream&amp; os, short places = default_places);
  502. <a href="#auto_cpu_timer-6">auto_cpu_timer</a>(std::ostream&amp; os, const std::string&amp; format);
  503. <a href="#auto_cpu_timer-destructor">~auto_cpu_timer</a>() noexcept;
  504. // compiler generated; shown for exposition only
  505. auto_cpu_timer(const auto_cpu_timer&amp;) = default;
  506. auto_cpu_timer&amp; operator=(const auto_cpu_timer&amp;) = default;
  507. // <a href="#auto_cpu_timer-observers">observers</a>
  508. std::ostream&amp; <a href="#ostream">ostream</a>() const noexcept;
  509. short <a href="#places">places</a>() const noexcept;
  510. const std::string&amp; <a href="#format_string">format_string</a>() const noexcept;
  511. // <a href="#auto_cpu_timer-actions">actions</a>
  512. void <a href="#report">report</a>();
  513. };</pre>
  514. </td>
  515. </tr>
  516. </table>
  517. <p dir="ltr">[<i>Note:</i> Constructors without a <code>std::ostream&amp;</code>
  518. argument argument imply <code>
  519. std::cout</code>. An argument default is avoided as it would require including <code>&lt;iostream&gt;</code>,
  520. with its high costs, even when the standard streams are not used. <i>--end note</i>]</p>
  521. <h3><a name="auto_cpu_timer-constructors"><code>auto_cpu_timer</code> constructors</a></h3>
  522. <pre><span style="background-color: #D7EEFF">explicit <a name="auto_cpu_timer-1">auto_cpu_timer</a>(short</span><span style="background-color: #D7EEFF"> places = </span><span style="background-color: #D7EEFF">default_places</span><span style="background-color: #D7EEFF">);
  523. <a name="auto_cpu_timer-2">auto_cpu_timer</a>(short</span><span style="background-color: #D7EEFF"> places, const std::string&amp; format);
  524. explicit <a name="auto_cpu_timer-3">auto_cpu_timer</a>(const</span><span style="background-color: #D7EEFF"> std::string&amp; format);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  525. <a name="auto_cpu_timer-4">auto_cpu_timer</a>(std::ostream</span><span style="background-color: #D7EEFF">&amp; </span><span style="background-color: #D7EEFF">os</span><span style="background-color: #D7EEFF">, short places, const std::string&amp; format);<br>explicit <a name="auto_cpu_timer-5">auto_cpu_timer</a>(std::ostream</span><span style="background-color: #D7EEFF">&amp; </span><span style="background-color: #D7EEFF">os</span><span style="background-color: #D7EEFF">, short places = </span><span style="background-color: #D7EEFF">default_places</span><span style="background-color: #D7EEFF">);<br> <a name="auto_cpu_timer-6">auto_cpu_timer</a>(std::ostream</span><span style="background-color: #D7EEFF">&amp; </span><span style="background-color: #D7EEFF">os</span><span style="background-color: #D7EEFF">, const std::string&amp; format);
  526. </span></pre>
  527. <blockquote>
  528. <p><i>Effects:</i> Constructs an object of type <code>
  529. auto_cpu_timer</code> and stores the ostream, places, and format string data
  530. needed to establish the postconditions.</p>
  531. <p><i>Postconditions:</i></p>
  532. <ul>
  533. <li>For overloads with an <code>os</code> argument, <code>ostream() == os</code>.
  534. Otherwise <code>ostream() == std::cout</code>.</li>
  535. <li><code>places() == places</code>.</li>
  536. <li>For overloads with a <code>format</code> argument, <code>format_string()
  537. == format</code>. Otherwise <code>format_string() == std::cout</code></li>
  538. </ul>
  539. </blockquote>
  540. <h3><a name="auto_cpu_timer-destructor"><code>auto_cpu_timer</code> destructor</a></h3>
  541. <pre><span style="background-color: #D7EEFF">~</span><span style="background-color: #D7EEFF">auto_cpu_timer</span><span style="background-color: #D7EEFF">() noexcept;</span></pre>
  542. <blockquote>
  543. <p dir="ltr"><i>Effects: </i>If <code>!is_stopped()</code>, stop(), <a href="#report">
  544. report()</a>.</p>
  545. <p dir="ltr">[<i>Note:</i> Because the function is <code>noexcept</code>,
  546. implementation must ensure no exception
  547. escapes. <i>--end note</i>]</p>
  548. </blockquote>
  549. <h3><a name="auto_cpu_timer-observers">auto_cpu_timer observers</a></h3>
  550. <p>The observers allow testing of constructor postconditions and specification
  551. of other functionality without resorting to &quot;for exposition only&quot; private
  552. members.</p>
  553. <pre><span style="background-color: #D7EEFF">std::ostream&amp; <a name="ostream">ostream</a>() const noexcept;</span></pre>
  554. <blockquote>
  555. <p><i>Returns:</i> The ostream stored by construction or subsequent copy
  556. assignment.</p>
  557. </blockquote>
  558. <pre><span style="background-color: #D7EEFF">short <a name="places">places</a>() const noexcept;</span></pre>
  559. <blockquote>
  560. <p><i>Returns:</i> The places stored by construction or subsequent copy
  561. assignment.</p>
  562. </blockquote>
  563. <pre><span style="background-color: #D7EEFF">const std::string&amp; <a name="format_string">format_string</a>() const noexcept;</span></pre>
  564. <blockquote>
  565. <p><i>Returns:</i> The format string stored by construction or subsequent copy
  566. assignment.</p>
  567. </blockquote>
  568. <h3><a name="auto_cpu_timer-actions"><code>auto_cpu_timer</code> actions</a></h3>
  569. <pre><span style="background-color: #D7EEFF">void <a name="report">report</a>();</span></pre>
  570. <blockquote>
  571. <p><i>Effects: </i>As if:</p>
  572. <blockquote>
  573. <pre>ostream() &lt;&lt; timer::format(elapsed(), places(), format_string());</pre>
  574. </blockquote>
  575. <p>[<i>Note: </i>It may be desirable to call <code>stop()</code> before
  576. calling <code>report()</code> because doing I/O while the
  577. timer is running might produce misleading results. <code>resume()</code> may
  578. be called afterwards to continue timing. <i>--end note</i>]</p>
  579. </blockquote>
  580. <h2><a name="History">History</a></h2>
  581. <p>Beman Dawes and Rob Stewart developed version 2 of the library.</p>
  582. <p>Beman did the initial development. Rob contributed many corrections, comments, and suggestions. In
  583. particular, he suggested the <code>resume()</code> and <code>format()</code>
  584. functions, resulting in improved ease-of-use for several use cases.</p>
  585. <h2><a name="Acknowledgements">Acknowledgements</a></h2>
  586. <p>Comments and suggestions came from Greg Rubino, Dave Abrahams, Vicente
  587. Botet, and John Maddock.</p>
  588. <hr>
  589. <p><font size="2">Revised:
  590. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B %Y" startspan -->08 October 2011<!--webbot bot="Timestamp" endspan i-checksum="32193" --></font></p>
  591. <p><font size="2">© Copyright Beman Dawes, 2006<br>
  592. © Copyright Beman Dawes and Robert Stewart, 2011</font></p>
  593. <p><font size="2">Distributed under the Boost Software License, Version 1.0. See <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/ LICENSE_1_0.txt</a></font></p>
  594. </body>
  595. </html>