0

I am trying to measure the running time in nanoseconds of a function by using clock() function, like this:

clock_t start_t = clock();  
random_function();  
clock_t end_t = clock();
printf("Elapsed time: %f\n", (double)(end_t - start_t) / CLOCKS_PER_SEC);

When I run the program I sometimes get very big negative numbers as a result. Why does this happen?

AN00
  • 325
  • 4
  • 13

1 Answers1

0

If your platform us using unsigned for clock_t you can use modulo std::numeric_limits<clock_t>::max() subtraction, instead of the absolute one

bobah
  • 18,364
  • 2
  • 37
  • 70