开发者

Timing program runtimes in visual C++

Is there a quick and easy way of timing a section of a program (or the entire thing) without havi开发者_如何学编程ng to setup a timer class, functions, and variables inside my program itself?

I'm specifically referring to Visual C++ (Professional 2008).

Thanks,

-Faken

Edit: none of these answers do what i ask for, i would like to be able to time a program inside visual c++ WITHOUT having to write extra bits of code inside it. Similar to how people do it with BASH in Linux.


timeGetTime gives you number of milliseconds passed since the machine was turned on. By calculating the difference between the results of this function before and after your operation, you get the number of milliseconds it took. Don't forget to link with winmm.lib

EDIT:

DWORD msBegin = timeGetTime();
// do your stuff here
DWORD msDuration = timeGetTime() - msBegin;
// at this moment msDuration contains number of milliseconds it took for your stuff to execute

Caveats:

  1. the resolution of this timer is usually 10msec. There are ways to change it, but it's accurate enough for most of the scenarios
  2. if you have several processors, the results returned by this function, executed on different processors may be inconsistent


Visual Studio has a profiler, but you might need a higher SKU than Pro to access it (e.g., Team System Development Edition). I'm not sure if the profiler does actual timings. From the linked article, it looks as though it might just do sampling, which can tell you which functions are taking the most time.

Back in the VC6 days, there were command line tools for profiling (PREP, PROFILE, and PREPORT). Those could do timing as well as sampling. Building a better profiler is great, but restricting it to the multi-thousand dollar SKUs keeps smaller ISVs and hobbyists out of the game.


In the Intel and AMD CPUs there is a high speed counter. The Windows API includes function calls to read the value of this counter and also the frequency of the counter - i.e. how many times per second it is counting.

Here's an example how to time your time in microseconds:


#include <iostream>
#include <windows.h>

int main()
{
        __int64 ctr1 = 0, ctr2 = 0, freq = 0;

        // Start timing the code.

        if (QueryPerformanceCounter((LARGE_INTEGER *) &ctr1) != 0) {
                // Do what ever you do, what ever you need to time...
                //
                //
                //

                // Finish timing the code.

                QueryPerformanceCounter((LARGE_INTEGER *) &ctr2);
                QueryPerformanceFrequency((LARGE_INTEGER *) &freq);

                // Print the time spent in microseconds to the console.

                std::cout << ((ctr2 - ctr1) * 1.0 / freq) << std::endl;
        }
}


This answer at superuser.com suggests Timethis.exe from Windows 2000 Resource Kit Tool (just like time for Unix systems). Example usage:

...

The programs were run with these commands:

timethis "perf.exe    tt > NUL"
timethis "perfgcc.exe tt > NUL"

The timings (with a file having 100 lines, each line containing 250000 integers more or less randomly distributed between 1 and 250000) were:

VS compiler:

  • 7.359
  • 7.359
  • 7.296

GCC:

  • 1.906
  • 1.906
  • 1.937

...

So Visual Studio is not involved at all.

Another such little utility is ptime.


If you want to time sections of your program without changing the code then you need to get hold of a profiler.

Visual Studio Team System (or Premium in VS2010) has a profiler but it's not available in Professional. Other well regarded options are AQTime (which has 30 day trial) and Redgate's ANTS profiler (which has a two week trial).

You may also want to look at the suggestions in this question for free options, which recommends Sleepy and AMD's CodeAnalyst for Windows.


In VS19 you can just start debugging and use PerfTips. The grey text in the end of the line will tell you how much time has elapsed since the last break. You can get a better picture at this by opening the Events tab in Diagnostic Tools window.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜