- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Measure execution time with high precision in C/C++
To create high precision timer we can use the chrono library. This library has high resolution clock. This can count in nanoseconds.
In this program we will see the execution time in nanoseconds. We will take the time value at first, then another time value at the last, then find the difference to get elapsed time. Here we are using blank loop to pause the effect for sometimes.
Example
#include <iostream> #include <chrono> typedef std::chrono::high_resolution_clock Clock; main() { auto start_time = Clock::now(); for(int i = 0; i<200000000; i++ ){ //create one empty loop ; } auto end_time = Clock::now(); std::cout << "Time difference: " << std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count() << " nanoseconds" << std::endl; }
Output
Time difference: 536395307 nanoseconds
- Related Articles
- How to measure time with high-precision in Python?
- How to measure the execution time in Golang?
- Measure execution time of small Python code snippets (timeit)
- How to measure execution time for a Java method?
- Calculate the execution time of a method in C#
- Execution of printf with ++ operators in C
- How to calculate Execution Time of a Code Snippet in C++?
- How to measure time taken by a function in C?
- How to measure elapsed time in nanoseconds with Java?
- How to find PHP execution time?
- How do I print a double value with full precision using cout in C++?
- What is the precision of floating point in C++?
- How to measure high resistances? (Resistance Measurement Methods)
- How to calculate elapsed/execution time in Java?
- How do we measure time?

Advertisements