-1

I'd like to ask, which method of measuring the time which is required for function to execute is considered the best? Just curious. Thanks.

Duosora
  • 83
  • 1
  • 1
  • 8
  • 1
    Beware that adding benchmarking code will alter the performance characteristics of your code. Especially if your function is faster than `gettimeofday`. Always make sure you look at the assembly to ensure that you are timing what you think you are timing. – Barry Nov 09 '14 at 23:21

1 Answers1

1

You can try to use rdtsc(); it's accurate, perhaps fastest and cross-platform (but only for x86 architecture). For example, in Windows:

#include <intrin.h>
uint64_t rdtsc()
{
return __rdtsc();
}
Alex
  • 36
  • 5
  • It's not accurate (unless correctly serialized), not cross platform (not everything is x86), and in general - very problematic. – Leeor Nov 11 '14 at 08:43
  • Right, it's very critical to serialization, better to use with cpu affinity and all power management turned off... But why you considered it very problematic?:) – Alex Nov 11 '14 at 12:31
  • I meant serialization to prevent the CPU from reordering it with any instruction block in the measured section or out of it. That is, in addition to any movement caused by the compiler. – Leeor Nov 11 '14 at 14:32