![]() |
VOOZH | about |
Prerequisite:Chrono Library in C++
The task is to calculate the running time of a program in Microseconds in C++
Header file:
#include<chrono>The Chrono library handles operations relating to time and date. This library deals with the fact that timers and clocks might be different on different systems and thus improve over time in terms of precision.
Chrono is both a header file and even a namespace. All the elements in this header (except for the common_type specializations) are not defined directly under the std namespace (like most of the standard library) but under the std::chrono namespace.
Here function_performing_iterations() is a dummy function that iterates from i=1 to i=10^7. Now we start counting time by writing
auto duration = duration_cast<microseconds>(stop1 - start1);
Now when the compiler executes the function-> function_performing_iterations(), the control then again comes back to main(), after which the time is stopped. (For reference it can be assumed as a race i.e. when in a race as soon as the runners start running, the timer is started and when they reach the finish line, the timer is stopped)
Now the time is calculated as:
auto duration = duration_cast<microseconds>(stop1 - start1);
Finally, the time taken in microseconds is printed by type casting in double because the a/b normal division returns an integer value.
Example:
Output:
Time taken in microseconds : 16578Boost Libraries are intended to be widely useful, and usable across a broad spectrum of applications. For example, they are helpful for handling large numbers having a range beyond the long long, long double data type (264) in C++.
Installation:
Please refer to this Article for the installation of the boost. We can download the zip file. After that, we just need to extract the whole in a specified folder of GCC or we can do this easily by command prompt.
Example:
Output: