0

I am using time_taken = ((double)t * 1000) / CLOCKS_PER_SEC; Where

    t = clock();
    myFunctionToMeasureTimeTaken();
    t = clock() - t;

But at best it only give the running time in ms. And that function runs way too fast.

Is there anything I can use to measure in times of micro or even nanosec?

I am using MS Visual Studio 2013

Gavin
  • 2,784
  • 6
  • 41
  • 78
  • You could try a [high resolution timer](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644900(v=vs.85).aspx#high_resolution), though it's not portable. Or you could run the function a 1000 times and measure the average run-time instead. – Kninnug Oct 11 '15 at 13:35
  • http://stackoverflow.com/questions/13610471/calculating-function-time-in-nanoseconds-in-c-code – Iledran Oct 11 '15 at 13:36
  • Possible duplicate of [How to print time difference in accuracy of milliseconds and nanoseconds?](http://stackoverflow.com/questions/16275444/how-to-print-time-difference-in-accuracy-of-milliseconds-and-nanoseconds) – Itay Sela Oct 11 '15 at 13:36

1 Answers1

0

The usual way to time functions with short execution times, when only a coarse timer is available, is to run the function repeatedly in a loop, say, a million times, and measure that.

If you are willing to sacrifice Standard C conformance, there may be system specific timers with higher resolution.

Jens
  • 69,818
  • 15
  • 125
  • 179