C++ measure execution time: In the previous article, we have discussed about Convert Set to Vector. Let us learn How to Measure Execution Time in C++ Program.
In this post, we will look at how to calculate the execution time of any procedure in C++.
We will calculate execution time in seconds,minutes,hours,milliseconds,microseconds etc.
Calculate Execution Time in C++
C++ measure execution time milliseconds: We often experience situations in which we need to measure the execution time of a large number of operations in order to evaluate results. As an example,
- How long did it take to push data into a vector, and so on.
- How much time was spent looking for data in a datastructure?
- How long did it take to send or receive a message over the network, and so on.
We can use the boost::posix time::ptime and boost::posix time::time duration forms to accomplish this.
- boost::posix_time::ptime denotes a specific time point.
- boost::posix_time::time_duration denotes the length of time between two points.
To calculate execution time, we will retrieve the current time twice: once before and once after the process.
The difference between these two timestamps will give us the operation’s execution time, i.e.
- Start_Time =Get present Time
- Executing the code
- End_Time=Get present Time
- Time taken =End_Time-Start_Time
Assume our task is to insert 9000000 string values into string vector. Let’s look at how to calculate execution time using boost.
Below is the implementation:
#include <bits/stdc++.h>
#include <boost/date_time.hpp>
using namespace std;
int main()
{
// Creating a string vector
vector<string> stringvector;
// Get present Time before the task gets executed
boost::posix_time::ptime start_time
= boost::posix_time::second_clock::local_time();
// Adding 9000000 string values to vector
for (int i = 0; i < 9000000; i++)
stringvector.push_back(std::to_string(i));
// get the time after the task gets executed
boost::posix_time::ptime end_time
= boost::posix_time::second_clock::local_time();
// Get the time taken to execute the task by calculating
// diffrence between end time and start time
boost::posix_time::time_duration timetaken
= end_time - start_time;
cout << "Time taken = " << timetaken << endl;
// Printing the time taken in other formats
cout << "Time taken in hours : " << timetaken.hours()
<< endl;
cout << "Time taken in minutes : "
<< timetaken.minutes() << endl;
cout << "Time taken in seconds : "
<< timetaken.seconds() << endl;
// Printing the time taken to execute the task in total
// seconds only
cout << "Time taken in Total Seconds : "
<< timetaken.total_seconds() << endl;
// Printing the time taken to execute the task in total
// milliseconds only
cout << "Time taken in Total Milli Seconds : "
<< timetaken.total_milliseconds() << endl;
// Printing the time taken to execute the task in total
// microseconds only
cout << "Time taken in Total Micro Seconds : "
<< timetaken.total_microseconds() << endl;
// Printing the time taken to execute the task in total
// nanoseconds only
cout << "Time taken in Total Nano Seconds : "
<< timetaken.total_nanoseconds() << endl;
}
Output:
Time taken = 00:00:02 Time taken in hours : 0 Time taken in minutes : 0 Time taken in seconds : 2 Time taken in Total Seconds : 2 Time taken in Total Milli Seconds : 2000 Time taken in Total Micro Seconds : 2000000 Time taken in Total Nano Seconds : 2000000000
Related Programs:
- how to web scrape with python in 4 minutes
- how to create and initialize a list of lists in python
- how to append text or lines to a file in python
- python how to check if a key exists in dictionary
- python how to find all indexes of an item in a list
- how to comment in html css and javascript
- how to change background image opacity in css without affecting text html css