C++ Measure Execution Time with std::chrono
To measure execution time in C++, record std::chrono::high_resolution_clock::now() before and after your code, then subtract the two time points. The <chrono> library gives you precise, portable timing without any platform-specific code.
The Basic Pattern
Take a timestamp before and after the work, then convert the difference to whatever unit you want:
#include <iostream>
#include <chrono>
int main() {
auto start = std::chrono::high_resolution_clock::now();
// --- the code you want to time ---
long long sum = 0;
for (int i = 0; i < 10000000; i++) sum += i;
// ---------------------------------
auto end = std::chrono::high_resolution_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Took " << ms.count() << " ms\n";
return 0;
}
now() returns a time point. Subtracting two time points gives a duration, and duration_cast converts that duration into milliseconds. Calling .count() extracts the number so you can print it.
Getting Finer Resolution
If your code runs in under a millisecond, the result will show 0 ms. Switch the cast to microseconds (or nanoseconds) for more detail:
auto us = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "Took " << us.count() << " us\n";
The pattern is identical — only the unit type inside duration_cast changes. This is why <chrono> is so pleasant: you pick the unit at the moment you read the result.
Seconds as a Decimal
To show seconds with decimals (like 1.37 s), use a duration<double> instead of an integer cast:
std::chrono::duration<double> elapsed = end - start;
std::cout << "Took " << elapsed.count() << " s\n";
Here the duration stores a floating-point number of seconds, so .count() gives you 1.37 rather than a whole number.
A Reusable Timer
If you time things often, wrap the pattern in a small helper that times any function you pass it:
#include <iostream>
#include <chrono>
template <typename Func>
long long timeMs(Func work) {
auto start = std::chrono::high_resolution_clock::now();
work();
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
}
int main() {
long long ms = timeMs([]{
long long s = 0;
for (int i = 0; i < 10000000; i++) s += i;
});
std::cout << "Loop took " << ms << " ms\n";
return 0;
}
You pass the code to time as a lambda, and timeMs returns the elapsed milliseconds. This keeps your timing logic in one place instead of repeating now() calls everywhere.
Which Clock to Use
high_resolution_clock is fine for most beginner timing. For serious benchmarking prefer steady_clock — it’s guaranteed to never run backward, while system_clock can jump if the OS adjusts the wall-clock time. Never use system_clock to measure how long code takes.
Related Articles
- C++ Functions Tutorial — timing your own functions
- C++ Lambda Functions — passing code to the timer
- C++ Loops Tutorial — the kind of code you’ll measure
- C++ auto Keyword — why auto suits chrono types
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.