Excerpt execution time of a code with an accuracy greater than 0.001 seconds

Asked

Viewed 38 times

0

I need to compute the execution time of sorting algorithms. I did the following:

steady_clock::time_point t3 = steady_clock::now();  
quickSort(v, 0, n - 1);  
steady_clock::time_point t4 = steady_clock::now();  
duration<double> time_span = duration_cast<duration<double,nano>>(t4 - t3);

The problem is that for time I cannot compute times less than 0.001 seconds, the value of time_span appears equal to 0.

  • trade in steady_clock for high_resolution_clock solves something? I find it unlikely, but possible.

1 answer

0

I believe the problem is on the line

duration<double> time_span = duration_cast<duration<double,nano>>(t4 - t3);

You forgot to std::nano and lowered the resolution to seconds. That’s the reason I prefer to use auto in such cases. Try replacing with

  auto time_span = duration_cast<duration<double, nano>>(t4 - t3);
  • I did not put the nano to show the result in seconds, but to test I did the way you spoke using (auto) and still not getting a time below 0.001 seconds

Browser other questions tagged

You are not signed in. Login or sign up in order to post.