Measure execution time of a function in C++ Last Updated : 11 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We can find out the time taken by different parts of a program by using the std::chrono library introduced in C++ 11. We have discussed at How to measure time taken by a program in C. The functions described there are supported in C++ too but they are C specific. For clean and robust C++ programs we should strive to use C++ specific language constructs only.std::chrono has two distinct objects--timepoint and duration. A timepoint as the name suggests represents a point in time whereas a duration represents an interval or span of time. The C++ library allows us to subtract two timepoints to get the interval of time passed in between. Using provided methods we can also convert this duration to appropriate units.The std::chrono provides us with three clocks with varying accuracy. The high_resolution_clock is the most accurate and hence it is used to measure execution time. Step 1: Get the timepoint before the function is called CPP #include <chrono> using namespace std::chrono; // Use auto keyword to avoid typing long // type definitions to get the timepoint // at this instant use function now() auto start = high_resolution_clock::now(); Step 2: Get the timepoint after the function is called CPP #include <chrono> using namespace std::chrono; // After function call auto stop = high_resolution_clock::now(); Step 3: Get the difference in timepoints and cast it to required units CPP // Subtract stop and start timepoints and // cast it to required unit. Predefined units // are nanoseconds, microseconds, milliseconds, // seconds, minutes, hours. Use duration_cast() // function. auto duration = duration_cast<microseconds>(stop - start); // To get the value of duration use the count() // member function on the duration object cout << duration.count() << endl; A complete C++ program demonstrating the procedure is given below. We fill up a vector with some random numbers and measure the time taken by sort() function to sort this vector. CPP // C++ program to find out execution time of // of functions #include <algorithm> #include <chrono> #include <iostream> #include<vector> using namespace std; using namespace std::chrono; // For demonstration purpose, we will fill up // a vector with random integers and then sort // them using sort function. We fill record // and print the time required by sort function int main() { vector<int> values(10000); // Generate Random values auto f = []() -> int { return rand() % 10000; }; // Fill up the vector generate(values.begin(), values.end(), f); // Get starting timepoint auto start = high_resolution_clock::now(); // Call the function, here sort() sort(values.begin(), values.end()); // Get ending timepoint auto stop = high_resolution_clock::now(); // Get duration. Substart timepoints to // get duration. To cast it to proper unit // use duration cast method auto duration = duration_cast<microseconds>(stop - start); cout << "Time taken by function: " << duration.count() << " microseconds" << endl; return 0; } Output: (Machine Dependent) Time taken by function: 3062 microsecondsThe time complexity of the sort() function is O(n log n), where n is the number of elements in the vector. Therefore, the time complexity of this program is also O(n log n), since it measures the execution time of the sort() function. The space complexity of this program is O(n), since it creates a vector of size n to store the random integers.References https://www.geeksforgeeks.org/chrono-in-c/ Comment More infoAdvertise with us Next Article Measure execution time of a function in C++ S Sayan Mahapatra Follow Improve Article Tags : Competitive Programming C++ DSA CPP-Library cpp-puzzle +1 More Practice Tags : CPP Similar Reads strftime() function in C/C++ strftime() is a function in C which is used to format date and time. It comes under the header file time.h, which also contains a structure named struct tm which is used to hold the time and date. The syntax of strftime() is as shown below : size_t strftime(char *s, size_t max, const char *format, c 3 min read Find Exclusive Time of Functions Given a list logs, where logs[i] represents the ith log message formatted as a string "{function_id}:{"start" | "end"}:{timestamp}". For example, "0:start:3" means a function call with function ID 0 started at the beginning of timestamp 3, and "1:end:2" means a function call with function ID 1 ended 5 min read multiset count() function in C++ STL The multiset::count() function is a built-in function in C++ STL that searches for a specific element in the multiset container and returns the number of occurrences of that element. Syntax: multiset_name.count(val) Parameters: The function accepts a single parameter val which specifies the element 2 min read How to Measure Elapsed Time in C++? Measuring elapsed time is a common requirement for most software development packages. It is used to determine the efficiency of the program and gives an idea of which parts of the program takes which much time. This helps in optimizing the code, such that improves its execution time. In this articl 3 min read unordered_multimap count() function in C++ STL The unordered_multimap::count() is a built-in function in C++ STL that returns the number of elements in the container whose key is equal to the key passed in the parameter. Syntax: unordered_multimap_name.count(key) Parameters: The function accepts a single mandatory parameter key that specifies th 2 min read localtime() function in C++ The localtime() function is defined in the ctime header file. The localtime() function converts the given time since epoch to calendar time which is expressed as local time. Syntax: tm* localtime(const time_t* time_ptr); Parameter: This function accepts a parameter time_ptr which represents the poin 1 min read mktime() function in C++ STL The mktime() is an inbuilt C++ function which converts the local calendar time to the time since epoch and returns the value as an object of type time_t. Syntax : time_t mktime( struct tm *time_ptr ) Parameters: The function accepts a mandatory parameter pointer time_ptr that points to a tm object s 2 min read norm() function in C++ with Examples The norm() function is defined in the complex header file. This function is used to return the squared magnitude of the complex number z. Syntax: template<class T> T norm (const complex<T>& z); Parameter: z: It represents the given complex number. Return: It returns the squared magni 1 min read Sleep Function in C++ C++ provides the functionality of delay or inactive state of the program with the help of the operating system for a specific period of time. Other CPU operations will function adequately but the sleep() function in C++ will sleep the present executable for the specified time by the thread.The sleep 3 min read std::function in C++ The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s 5 min read Like