C++11 timing code performance
Posted on October 14, 2012 by Paul
Prior to the release of the C++11 standard, there was no standard way in which one could accurately measure the execution time of a piece of code. The programmer was forced to use external libraries like Boost, or routines provided by each operating system.
The C++11 chrono header file provides three standard clocks that could be used for timing one’s code:
- system_clock - this is the real-time clock used by the system;
- high_resolution_clock - this is a clock with the shortest tick period possible on the current system;
- steady_clock - this is a monotonic clock that is guaranteed to never be adjusted.
If you want to measure the time taken by a certain piece of code for execution, you should generally use the steady_clock, which is a monotonic clock that is never adjusted by the system. The other two clocks provided by the chrono header can be occasionally adjusted, so the difference between two consecutive time moments, t0 < t1, is not always positive.
The chrono header file provides us with some functions, to check the clock’s precision and if the particular implementation of a given clock is steady (meaning it is not adjustable). Since the implementation details of a particular clock depends on the compiler and the OS, it is always a good idea to test these properties:
This is the output of the above code on Mac OSX, compiled with clang-4.1:
On this machine, the steady_clock and high_resolution_clock have the same characteristics, both are non-adjustable clocks with nanosecond precision. On the other hand, the system_clock is adjustable and it has only microsecond precision.
For comparison, this is the output of the program, compiled with Visual Studio 2012, on a Windows 7 machine:
In this case, only the steady_clock is non-adjustable, the precision is the same for all three clocks.
For measuring the execution time of a piece of code, we can use the now() function:
If you want to print the time difference between start and end in the above code, you could use:
If you prefer to use nanoseconds, you will use:
The value of the diff variable can be also truncated to an integer value, for example, if you want the result expressed as:
If you are interested in learning more about the new C++11 syntax I would recommend reading The C++ Programming Language by Bjarne Stroustrup.
or, Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper: