Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Anvi Jain

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.