Measure execution time in C++

To measure the execution time in C++, there's a library called std::chrono that you can use. std::chrono offers two objects, a timepoint object and a duration object. The timepoint object is used to get a point in time, while the duration object can be used to get the interval between two timepoints. The library provides three different clocks with different accuracy: system_clocksteady_clock and high_resolution_clock, where the high_resolution_clock is the one with most accuracy and is best fit for measuring the execution time of algorithms. To get a timepoint out of high_resolution_clock, we call high_resolution_clock::now().

When we want to measure the execution time of an algorithm, we first store the value of std::chrono::high_resolution_clock::now() in a variable before the algorithm start.

auto start = std::chrono::high_resolution_clock::now();

Run the algorithm. In this example we are sorting a vector of random elements with std::sort.

std::sort(arr.begin(), arr.end());

Then we store a timepoint in a variable again after the algorithm is done.

auto stop = std::chrono::high_resolution_clock::now();

The duration object is then used to calculate the interval between the two points. To get the actual duration, remember to add .count() in the end as in the code below. 

auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();
std::cout << duration << std::endl;

If you want to get the result in something else than microseconds, here are the different types you can use:

std::chrono::nanoseconds
std::chrono::microseconds
std::chrono::milliseconds
std::chrono::seconds
std::chrono::minutes
std::chrono::hours

Here's how a program can look that measures the time execution of std::sort:

#include <iostream>
#include <vector>
#include <time.h>
#include <ctime>
#include <chrono>

int main() {
	srand(time(NULL));
	std::vector<int> arr(1000);

	std::generate(arr.begin(), arr.end(), []() {
		return rand() % 1000;
		});

	auto start = std::chrono::high_resolution_clock::now();

	std::sort(arr.begin(), arr.end());

	auto stop = std::chrono::high_resolution_clock::now();
	auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();

	std::cout << duration << std::endl;
	return 0;
}

 

Output (on my machine, you might get a different result):

1009

 

Author

authors profile photo

Articles with similar tags

thumbnail
C++ Overloading Relational Operators

This article goes through how to overload relational operators in C++. Create your own user-defined types and use overloaded operators with them.

By Frogitecture

thumbnail
C++ Inheritance - 1 - Base and Derived Classes

Inheritance is a form of software reuse in which a class can use another class’s methods and members and modify them.

By Frogitecture

thumbnail
C++ Inheritance – 2 – Protected Members

Two very common access specifier in C++ are public and private. Here I introduce the access specifier protected and give you an example of how you can use it.

By Frogitecture

thumbnail
C++ Inheritance – 3 – Constructors and Destructors

When a derived-class object is instantiated, a chain of constructor calls begins. Similarly, when a derived-class object is destroyed, destructor calls begins

By Frogitecture