Chapter 38. Boost.Timer
Since version 1.48.0 of the Boost libraries, there have been two versions of Boost.Timer. The second version of Boost.Timer has only one header file: . The library also ships a header file boost/timer.hpp
. Do not use this header file. It belongs to the first version of Boost.Timer, which shouldn’t be used anymore.
The clocks provided by Boost.Timer are implemented in the classes boost::timer::cpu_timer
and boost::timer::auto_cpu_timer
. boost::timer::auto_cpu_timer
is derived from boost::timer::cpu_timer
and automatically stops the time in the destructor. It then writes the time to an output stream.
Example 38.1 starts by introducing the class boost::timer::cpu_timer
. This example and the following examples do some calculations to make sure enough time elapses to be measurable. Otherwise the timers would always measure 0, and it would be difficult to introduce the clocks from this library.
Example 38.1. Measuring time with boost::timer::cpu_timer
Measurement starts when boost::timer::cpu_timer
is instantiated. You can call the member function format()
at any point to get the elapsed time. displays output in the following format: 0.099170s wall, 0.093601s user + 0.000000s system = 0.093601s CPU (94.4%)
.
CPU time is divided between time spent in user space and time spent in kernel space. Kernel space refers to code that is part of the operating system. User space is code that doesn’t belong to the operating system. User space includes your program code and code from third-party libraries. For example, the Boost libraries are included in user space. The amount of time spent in kernel space depends on the operating system functions called and how much time those functions need.
Example 38.2. Stopping and resuming timers
boost::timer::cpu_timer
provides the member functions stop()
and , which stop and resume timers. In Example 38.2, the timer is stopped before the second for
loop runs and resumed afterwards. Thus, the second for
loop isn’t measured. This is similar to a stop watch that is stopped and then resumed after a while. The time returned by the second call to format()
in is the same as if the second for
loop didn’t exist.
boost::timer::cpu_timer
also provides a member function start()
. If you call start()
, instead of resume()
, the timer restarts from zero. The constructor of boost::timer::cpu_timer
calls start()
, which is why the timer starts immediately when boost::timer::cpu_timer
is instantiated.
Example 38.3. Getting wall and CPU time as a tuple
boost::timer::times
provides the member function clear()
to set wall, user, and system to 0.
Example 38.4. Measuring times automatically with boost::timer::auto_cpu_timer
You can measure the wall and CPU time of a code block with boost::timer::auto_cpu_timer
. Because the destructor of this class stops measuring time and writes the time to the standard output stream, Example 38.4 does the same thing as .
boost::timer::autocpu_timer
provides several constructors. For example, you can pass an output stream that will be used to display the time. By default, the output stream is _std::cout.
You can specify the format of reported times for boost::timer::auto_cpu_timer
and boost::timer::cpu_timer
. Boost.Timer provides format flags similar to the format flags supported by Boost.Format or std::printf()
. The documentation contains an overview of the format flags.