2

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.

Chad
  • 2,938
  • 3
  • 27
  • 38
Faken
  • 11,352
  • 17
  • 59
  • 74
  • 1
    You excluded specifically " *.. having to setup a timer class, functions, and variables inside my program itself?* " in your question then you accepted an answer which use QueryPerformanceCounter. A little weird... (euphemism) – MaD70 Nov 02 '09 at 08:20
  • Yea, bounty was ending in 2 hours and no one has posted an answer that was exactly what i wanted (which is likely visual studio's fault, it probably just doesn't have that feature). I've already forfeited some points for the bounty, which only gets half awarded to the highest voted answer even if I don't like the answer. So I mark the answer that i do like and award the points to that person. Better option than just letting points go to waste! – Faken Nov 04 '09 at 00:44

6 Answers6

3

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
  • 4,129
  • 23
  • 18
  • 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 Oct 22 '09 at 01:55
  • 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 Oct 22 '09 at 01:57
  • 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 Oct 22 '09 at 02:01
  • well, that means that you need to link your application with winmm.lib, as the article which I linked to, says – Rom Oct 22 '09 at 02:14
  • It might be easier to use GetTickCount() which exists in kernel32.lib and therefore doesn't need any additional import library. – Greg Hewgill Oct 22 '09 at 02:20
  • 2
    @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 Oct 22 '09 at 02:24
  • Nice trick, didn't know about this function, but it's definitely very useful for timing measures. – Pavel Minaev Oct 26 '09 at 18:06
3

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
  • 45,555
  • 16
  • 123
  • 175
2

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;
        }
}

nhaa123
  • 9,570
  • 11
  • 42
  • 63
  • 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 Nov 01 '09 at 23:27
  • If the function succeeds, the return value is non-zero. Otherwise you can get the error information with GetLastError(). – nhaa123 Nov 02 '09 at 01:33
  • 1
    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 Jan 29 '10 at 04:29
2

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.

Community
  • 1
  • 1
MaD70
  • 4,078
  • 1
  • 25
  • 20
2

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.

Community
  • 1
  • 1
Ian G
  • 10,709
  • 6
  • 30
  • 34
0

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.

Iizuki
  • 354
  • 1
  • 6
  • 12