2

I'm new to visual studio 2012 and I want to check the speed of my functions. What is the best way to do it? I am trying to speed up my function and I want to see the time it takes from the execution till it produces results. I appreciate the help.

Hell Man
  • 833
  • 3
  • 18
  • 24
  • Use the win32 PerformanceCounter: [How to use QueryPerformanceCounter?][1] [1]: http://stackoverflow.com/a/1739265/2930642 – eranb Dec 10 '13 at 05:07

2 Answers2

1

There is likely no good way. Depending on the version of Visual Studio you have, it may come with profiler support, if so, enable that and it will tell you.

Else, look at something like the high-performance counter API at: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904%28v=vs.85%29.aspx

RichardPlunkett
  • 2,998
  • 14
  • 14
1

In addition to using QueryPerformanceCounter mentioned in the other posts, there a few steps that will help you measure time more accurately:

  1. Call Sleep(0) or SwitchToThread() before starting time measurement. This will produce more reliable meausrements as context switches will not occur immediately after a return from Sleep(). If the function is short and called in a tight loop, call Sleep before the loop, you want the CPU caches to remain HOT when your function is called.
  2. Raise the priority of the thread and/or the base priority of the process via SetThreadPriority and SetProcessPriorityBoost.
  3. store N thousand of results in an array, throw away or ignore worst 5% (outliers). Before printing the stats, sort the array so it's easier to find the outliers.
egur
  • 7,830
  • 2
  • 27
  • 47