1

A common way to measure elapsed time is:

const clock_t START = clock();
// ...
const clock_t END = clock();
double T_ELAPSED = (double)(END - START) / CLOCKS_PER_SEC;

I know this is not the best way to measure real time, but I wonder if it works on a system with a variable frequency CPU. Is it just wrong?

Narcolessico
  • 1,921
  • 3
  • 19
  • 19
  • Presumably, CLOCKS_PER_SEC is defined within a header file as a constant value. If the underlying parameter represented by the value can change and you don't change the constant accordingly then, no, your elapsed time measurement won't be correct. – sizzzzlerz Nov 24 '10 at 16:45
  • If you're doing benchmarking then it's best to set the CPU frequency governor to "performance", at least temporarily, so that you can get accurate measurements, unskewed by clock speed variations. – Paul R Nov 24 '10 at 16:48

2 Answers2

1

It is not good to use on a variable clock speed CPU.

http://support.ntp.org/bin/view/Support/KnownHardwareIssues

NTP (network time prototcol) daemon on linux had issues with it.

Most OS's have some API calls for more accurate values, an example being on windows, QueryPerformanceCounter

http://msdn.microsoft.com/en-us/library/ms644904%28VS.85%29.aspx

Paul
  • 2,729
  • 20
  • 26
1

There are system architectures that change the frequency of the CPU but have a separate and constant frequency to drive a system clock. One would think that a clock() function would return a time independent of the CPU frequency but this would have to be verified on each system the code is intended to run on.

semaj
  • 1,555
  • 1
  • 12
  • 25