Hell Man
Hell Man

Reputation: 873

How can I check the function run time in Visual studio C++?

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.

Upvotes: 2

Views: 3054

Answers (2)

egur
egur

Reputation: 7960

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.

Upvotes: 1

RichardPlunkett
RichardPlunkett

Reputation: 2988

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

Upvotes: 1

Related Questions