In this article we will discuss how to measure execution time of any operation in C++.
Many time we encounter a situation, where we need to calculate the execution time of many operations to check performance. For example,
- How much time was taken to push data in a vector etc.
- How much time spent in searching data from a datastructure.
- How much time was taken to send or receive a message through network etc.
To do that we will use boost::posix_time::ptime and boost::posix_time::time_duration types.
boost::posix_time::ptime represents a particular time point
boost::posix_time::time_duration represents a duration / difference between two time points.
Calculating Execution / Elapsed Time
To Measure execution time we will fetch the current time 2 times, one before the operation and one after the operation.
Difference between these two timestamps will give us the execution time of the operation i.e.
Start Time = Get Current Time
Execute Task
End Time = Get Current Time
Time Duration = End Time – Start Time
Frequently Asked:
Suppose our task is to insert 9 Millions strings to a vector of strings. Let’s see how to measure the execution time using
above algo and ptime & time_duration i.e.
// Get the current time before executing Task boost::posix_time::ptime start = boost::posix_time::second_clock::local_time(); // Add 9 Million Strings to vector for(int i=0; i <9000000; i++) vectOfInts.push_back(std::to_string(i)); // Get the current time after executing Task boost::posix_time::ptime end = boost::posix_time::second_clock::local_time(); // Get the Time Difference by subtracting start time from end time boost::posix_time::time_duration dur = end - start;
Now from boost::posix_time::time_duration we can get hours, minutes & Second field using its member functions i.e.
// Getting Hours, Minutes & Second fields from Time Duraton Object std::cout<<"Time Diff - Getting Hours Field"<<dur.hours()<<std::endl; std::cout<<"Time Diff - Getting Minutes Field "<<dur.minutes()<<std::endl; std::cout<<"Time Diff - Getting Seconds Field "<<dur.seconds()<<std::endl;
Total Time Elapsed in milliseconds, microseconds & nanoseconds
We can also get the total time difference in milliseconds, microseconds & Nano seconds i.e.
Getting Total Execution Time in MilliSeconds
// Getting the Time Difference in Total Milli Seconds only std::cout<<"Time Diff in Total Milli Seconds "<<dur.total_milliseconds()<<std::endl;
Getting Total Execution Time in MicroSeconds
// Getting the Time Difference in Total Micro Seconds only std::cout<<"Time Diff in Total Micro Seconds "<<dur.total_microseconds()<<std::endl;
Getting Total Execution Time in NanoSeconds
// Getting the Time Difference in Total Nano Seconds only std::cout<<"Time Diff in Total Nano Seconds "<<dur.total_nanoseconds()<<std::endl;
Complete working example is as follows,
#include <boost/date_time.hpp> #include <iostream> #include <algorithm> #include <vector> #include <string> int main() { // Get the time spent in adding 9 Million Strings in a vectir std::vector<std::string> vectOfInts; // Get the current time before executing Task boost::posix_time::ptime start = boost::posix_time::second_clock::local_time(); // Add 9 Million Strings to vector for(int i=0; i <9000000; i++) vectOfInts.push_back(std::to_string(i)); // Get the current time after executing Task boost::posix_time::ptime end = boost::posix_time::second_clock::local_time(); // Get the Time Difference by subtracting start time from end time boost::posix_time::time_duration dur = end - start; std::cout<<"Time Diff = "<<dur<<std::endl; // Getting Hours, Minutes & Second fields from Time Duraton Object std::cout<<"Time Diff - Getting Hours Field"<<dur.hours()<<std::endl; std::cout<<"Time Diff - Getting Minutes Field "<<dur.minutes()<<std::endl; std::cout<<"Time Diff - Getting Seconds Field "<<dur.seconds()<<std::endl; // Getting the Time Difference in Total Seconds only std::cout<<"Time Diff in Total Seconds "<<dur.total_seconds()<<std::endl; // Getting the Time Difference in Total Milli Seconds only std::cout<<"Time Diff in Total Milli Seconds "<<dur.total_milliseconds()<<std::endl; // Getting the Time Difference in Total Micro Seconds only std::cout<<"Time Diff in Total Micro Seconds "<<dur.total_microseconds()<<std::endl; // Getting the Time Difference in Total Nano Seconds only std::cout<<"Time Diff in Total Nano Seconds "<<dur.total_nanoseconds()<<std::endl; }
Output:
Time Diff = 00:00:03 Time Diff - Getting Hours Field0 Time Diff - Getting Minutes Field 0 Time Diff - Getting Seconds Field 3 Time Diff in Total Seconds 3 Time Diff in Total Milli Seconds 3000 Time Diff in Total Micro Seconds 3000000 Time Diff in Total Nano Seconds 3000000000
To compile the above code use following command in linux,
Pointers in C/C++ [Full Course]
g++ --std=c++11 example.cpp -lboost_date_time