Before C++11, there was no standard way to accurately measure the execution time of a piece of code. Programmers are forced to use external libraries such as boost or functions provided by the operating system.
The C++11 chrono header file provides three standard clocks for timing:
system_clock - Real-time clock provided by the system
high_resolution_clock - The clock with the shortest clock cycle in the current system
steady_clock - a monotonic clock that will not be adjusted
If you want to measure the execution time of a piece of code, you should basically use steady_clock, which is a monotonous clock that will not be adjusted by the system. The other two clocks of chrono are occasionally adjusted, so the difference between two consecutive time points t0 < T1 is not always positive.
The chrono header file provides functions to see the accuracy of the clock and whether the specific implementation of a given clock is stable (i.e., not adjusted). Because the implementation details of a particular clock depend on the compiler and operating system, it is always good to test these attributes:
#include <iostream> #include <chrono> using namespace std; int main(){ cout << "system_clock" << endl; cout << chrono::system_clock::period::num << endl; cout << chrono::system_clock::period::den << endl; cout << "steady = " << boolalpha << chrono::system_clock::is_steady << endl << endl; cout << "high_resolution_clock" << endl; cout << chrono::high_resolution_clock::period::num << endl; cout << chrono::high_resolution_clock::period::den << endl; cout << "steady = " << boolalpha << chrono::high_resolution_clock::is_steady << endl << endl; cout << "steady_clock" << endl; cout << chrono::steady_clock::period::num << endl; cout << chrono::steady_clock::period::den << endl; cout << "steady = " << boolalpha << chrono::steady_clock::is_steady << endl << endl; return 0; }
The above code is compiled with clang-4.1 in MacOSX and the output is as follows:
system_clock 1 1000000 steady = false high_resolution_clock 1 1000000000 steady = true steady_clock 1 1000000000 steady = true
On this machine, steady_clock and high_resolution_clock have the same characteristics, both of which are nanosecond precision and can not be adjusted. On the other hand, system_clock can be adjusted with only microsecond accuracy.
For comparison, the following is the program output compiled using Visual Studio 2012 on Windows 7:
system_clock 1 10000000 steady = false high_resolution_clock 1 10000000 steady = false steady_clock 1 10000000 steady = true
In this case, only steady_clock is not adjustable and the accuracy of the three clocks is the same.
To measure the execution time of a piece of code, we can use the now() function:
auto start = chrono::steady_clock::now(); // // Insert the code that will be timed // auto end = chrono::steady_clock::now(); // Store the time difference between start and end auto diff = end - start;
If you want to print the difference between start and end, you can use:
cout << chrono::duration <double, milli> (diff).count() << " ms" << endl;
If you prefer nanoseconds, you use:
cout << chrono::duration <double, nano> (diff).count() << " ns" << endl;
The value of the diff variable can be truncated into integers as follows:
diff_sec = chrono::duration_cast<chrono::nanoseconds>(diff); cout << diff_sec.count() << endl;