How to Calculate Running Time in Microseconds in C++?
Last Updated :
16 Sep, 2024
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.
Use of duration_cast:
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:
C++
// C++ to calculate time in microseconds
#include <chrono>
#include <iostream>
using namespace std;
// this namespace is used
// to perform calculation
using namespace chrono;
// A function performing 10^7 operations
void function_performing_iterations()
{
for (int i = 0; i < 10000000;) {
i++;
}
}
// Driver Code
int main()
{
// Timer starts now
auto start1 = high_resolution_clock::now();
function_performing_iterations();
// Timer ends now
auto stop1 = high_resolution_clock::now();
// Difference is calculated
auto duration
= duration_cast<microseconds>(stop1 - start1);
// Displays time in output file in
// microseconds
cerr << "Time taken in microseconds : " << duration.count() << endl;
}
Output:
Time taken in microseconds : 16578
By using the Boost library
Boost 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:
C++
// Cpp to calculate
// Running time in Microseconds
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
int main() {
// The starting time before performing the task
boost::posix_time::ptime start = boost::posix_time::second_clock::local_time();
for (int i = 1; i <= 10000000; i++)
{
//Performing Random task ...iterations from 1 to 10^7
}
// Getting the current time after executing Task
boost::posix_time::ptime end = boost::posix_time::second_clock::local_time();
// Calculating the difference by performing (end time - start time)
boost::posix_time::time_duration dur = end - start;
//Calculation time in microseconds
std::cout << "Time Diff in Total Micro Seconds " << dur.total_microseconds() << std::endl;
}
Output:
Similar Reads
How to Get Time in Milliseconds in C++? In programming measuring accurate time is a common requirement for most software development packages. In C++, we have the std::chrono library with various functions for durations, time, and clocks. In this article, we will learn how to get time in milliseconds in C++. For Example, Input:Current sys
2 min read
How to Round a Double to an int in C++? In C++, the double data type is used to store floating-point numbers with double precision. In this article, we will learn how to round a double value to an int in C++. Example: Input: double val= 2.6 Output: Integer for double value 2.5 is: 3 Rounding a Double to an Int in C++To round a double valu
2 min read
How to Sleep for Milliseconds in C++? In programming, sleeping for milliseconds means pausing the execution of the current thread for a specified number of milliseconds, it is often used in cases like creating delays, controlling the execution speed of a program, or simulating a time-consuming operation. In this article, we will learn h
2 min read
Measure execution time with high precision in C/C++ Execution time : The execution time or CPU time of a given task is defined as the time spent by the system executing that task in other way you can say the time during which a program is running. There are multiple way to measure execution time of a program, in this article i will discuss 5 differen
8 min read
How to Make a Countdown Timer in C++? In C++, countdown timers are valuable components in various applications, aiding in scheduling events, controlling timeouts, or displaying the remaining time. In this article, we will learn how to make a countdown timer in C++. Example: Input: Enter Total Number of Seconds for Countdown Timer: 3Outp
3 min read