Calculating the Execution Time of the C++ Program in Microseconds
Unfortunately, there are no methods for figuring out the precise execution time that works for everyone. For that, you need to choose the solution according to the situation. Measuring the accurate execution time of a c++ program can be difficult in some situations due to portability issues. Selecting the right methods that fit with your operating system and compiler version is essential to get an accurate time. More importantly, you need to understand what exactly you mean by the ‘time’ so that you can choose the method accordingly.
Here, you will find a comprehensive list of some easy, simple, and best options available to find the execution time of a c++ program. We will also list the limitations of the functions(s) to give you a heads-up before you start using that function in your program.
Hopefully, you will get the best option perfectly suitable for your operating system and compiler version.
Example 1:
We will examine Wall Time with the <chrono> function. The <chrono> function is one of the most portable ways to find the wall time in a c++ program. It works only with the c++11 and later versions. If you have an older version, upgrade your compiler before using this function. It has access to various types of clocks in the machine having different characteristics and purposes.
Here, we are using the high_resolution_clock to find the wall time as it uses the highest resolution clock available which is probably adequate for most systems. Refer to the code given below:
#include <chrono>
int main () {
double s = 0, a = 1;
auto start = std::chrono::high_resolution_clock::now();
int it = 1000*1000*1000;
for (int i=0; i<it; i++) {
s += a;
a /= 2.0;
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast(end - start);
printf("The clock begins at = %.2f\n", s);
printf("Measured time is = %.3f seconds.\n", elapsed.count() * 1e-6);
return 0;
}
The program starts with two header files stdio.h and chrono. The stdio.h library is used in conjunction with the standard input and output methods. The chrono library allows you to use the <chrono> function in your program. The whole program is written in the main function where all the execution happens.
The main program starts with initializing two variables ‘s’, and ‘a’ which will be used to keep track of the time. The ‘start’ variable is initialized with the current time with ‘chrono::high_reolution_clock::now()’ function.
The ‘it = 1000*1000*1000’ variable is initialized with the number of iterations that needs to be performed and calculates the elapsed time in microseconds. The following action will be carried out by the “for” loop as it iterates from 0 to 1000*1000*1000 times.
Another variable ‘end’ is initialized with the current time to check the elapsed time. When you calculate ‘end – start’ you will get the elapsed time of execution. The ‘chrono::microseconds’ will provide the execution time in microseconds that will be converted into seconds with ‘elapsed.count()*1e-6’ computation. The printf() statements will print the start time and elapsed time. And finally, the return 0 statement ensures the program is executed successfully.
When you run the complete code, a similar output will be produced depending on the execution speed and efficiency of your system:
Example 2:
In the code given below, we are using the gettimeofday() function to find the elapsed time since Epoch time is ‘1st, January 1970, 00:00:00 UTC’. The gettimeofday() function returns the time in both microseconds and seconds. Hence, to find the accurate time in microseconds, both times need to be summed together. Below attached code will help you calculate the execution time in microseconds using <sys/time.h and gettimeofday().
#include <sys/time.h>
int main () {
double s = 0,a = 1;
struct timeval start, end;
gettimeofday(&start, 0);
int it = 1000*1000*1000;
for (int i=0; i<it; i++) {
s += a;
a /= 2.0;
}
gettimeofday(&end, 0);
long sec = end.tv_sec - start.tv_sec;
long micro = end.tv_usec - start.tv_usec;
double elapsed = sec + micro*1e-6;
printf("The clock begins at = %.20f\n", s);
printf("Measured time is = %.3f seconds.\n", elapsed);
return 0;
}
The gettimeofday() function is included in the <sys/time.h> header file.
The ‘struct timeval’ is a timeval structure used to represent the elapsed time.
The computed time in seconds and microseconds are added to get the exact execution time in microseconds. The ‘tv_sec’ function provides the time in seconds and ‘tv_usec’ provides the time in microseconds. To convert the seconds into microseconds, multiply the microseconds by 1e-6. After that, add both times in seconds and microseconds to get the precise execution time in microseconds.
Now, click the compile and run icon in the icon bar of dev c++ software. Press the f11 key on the keyboard to execute the program. The executed output is given below:
Example 3:
In this example, we are using the <time.h> library and its time() function. The time() function works similarly to the gettimeofday() function providing the elapsed time since Epoch time. However, it only measures full seconds and you can’t define the time zone. Hence, if the measured intervals is more than a second then time() function makes sense. This might not be the first preference to get the microseconds and you can use other functions instead. See the code below:
#include <time.h>
int main () {
double s = 0, a = 1;
time_t start, end;
time(&start);
int it = 1000*1000*1000;
for (int i=0; i<it; i++) {
s += a;
a /= 2.0;
}
time(&end);
time_t elapsed = end - start;
printf("Result: %.20f\n", s);
printf("Time measured: %ld seconds.\n", elapsed);
return 0;
}
The time() function will provide the start and end time of execution and elapsed time can easily be calculated with an ‘end – start’ statement. See the result below:
Note that the elapsed time calculated with the time() function is 6s, not the exact 6.833 microseconds. The difference in the result of the time() function is seen in the output.
Conclusion
Various methods compute the execution time of the system using a C++ program. However, no solution is perfect for every situation and each function performs differently in different situations. Here we learned to use the time(), gettimeofday(), and <chrono> functions to get the execution time in microseconds.