C++11 timing code performance

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:

1 #include <iostream> 2 #include <chrono> 3 4 using namespace std ; 5 6 int main (){ 7 cout << "system_clock" << endl ; 8 cout << chrono :: system_clock :: period :: num << endl ; 9 cout << chrono :: system_clock :: period :: den << endl ; 10 cout << "steady = " << boolalpha << chrono :: system_clock :: is_steady << endl << endl ; 11 12 cout << "high_resolution_clock" << endl ; 13 cout << chrono :: high_resolution_clock :: period :: num << endl ; 14 cout << chrono :: high_resolution_clock :: period :: den << endl ; 15 cout << "steady = " << boolalpha << chrono :: high_resolution_clock :: is_steady << endl << endl ; 16 17 cout << "steady_clock" << endl ; 18 cout << chrono :: steady_clock :: period :: num << endl ; 19 cout << chrono :: steady_clock :: period :: den << endl ; 20 cout << "steady = " << boolalpha << chrono :: steady_clock :: is_steady << endl << endl ; 21 22 return 0 ; 23 }

This is the output of the above code on Mac OSX, compiled with clang-4.1:

1 system_clock 2 1 3 1000000 4 steady = false 5 6 high_resolution_clock 7 1 8 1000000000 9 steady = true 10 11 steady_clock 12 1 13 1000000000 14 steady = true

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:

1 system_clock 2 1 3 10000000 4 steady = false 5 6 high_resolution_clock 7 1 8 10000000 9 steady = false 10 11 steady_clock 12 1 13 10000000 14 steady = true

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:

1 auto start = chrono :: steady_clock :: now (); 2 3 // 4 // Insert the code that will be timed 5 // 6 7 auto end = chrono :: steady_clock :: now (); 8 9 // Store the time difference between start and end 10 auto diff = end - start ;

If you want to print the time difference between start and end in the above code, you could use:

1 cout << chrono :: duration < double , milli > ( diff ). count () << " ms" << endl ;

If you prefer to use nanoseconds, you will use:

1 cout << chrono :: duration < double , nano > ( diff ). count () << " ns" << endl ;

The value of the diff variable can be also truncated to an integer value, for example, if you want the result expressed as:

1 diff_sec = chrono :: duration_cast < chrono :: nanoseconds > ( diff ); 2 cout << diff_sec . count () << endl ;

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: