views:

296

answers:

5

Hello everyone,

Is there a quick and easy way of timing a section of a program (or the entire thing) without having 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.

+3  A: 

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
Rom
Um...how do i use it? Unfortunately I've only really learned what I needed to know to do what I needed to do...other than that, I know nothing...
Faken
DWORD msBegin = timeGetTime(); /* do your stuff */ DWORD msDuration = timeGetTime() - msBegin; // at this moment msDuration contains number of milliseconds it took for your stuff to execute
Rom
Ahh...it doesn't like that...I'm getting this error: 1>main.obj : error LNK2019: unresolved external symbol __imp__timeGetTime@0 referenced in function _main, will i have to put this into a class for it to work? If I do, that kind of defeats the purpose of this question...
Faken
well, that means that you need to link your application with winmm.lib, as the article which I linked to, says
Rom
It might be easier to use GetTickCount() which exists in kernel32.lib and therefore doesn't need any additional import library.
Greg Hewgill
@Greg Newgill: it's unstable, see for example http://blogs.msdn.com/larryosterman/archive/2009/09/02/what-s-the-difference-between-gettickcount-and-timegettime.aspx
Rom
Nice trick, didn't know about this function, but it's definitely very useful for timing measures.
Pavel Minaev
+2  A: 
nhaa123
This one is excellent for detailed timing.
Marcus Lindblom
Question: why do i need to check if the counter is currently not at zero? Other than that, this is the most simple and accurate solution (even though it technically doesn't fully answer my question).
Faken
If the function succeeds, the return value is non-zero. Otherwise you can get the error information with GetLastError().
nhaa123
Except for the cases when OS preempts your function and the second QPC() call is scheduled on a different processor. Now the fun begins...
Rom
+2  A: 

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.

Adrian McCarthy
+2  A: 

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.

MaD70
+1  A: 

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.

Ian G