Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 242
Faken Avatar asked May 24 '26 15:05

Faken


2 Answers

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
like image 91
Rom Avatar answered May 26 '26 04:05

Rom


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.

like image 25
Adrian McCarthy Avatar answered May 26 '26 03:05

Adrian McCarthy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!