Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual C++ Stopwatch

I wonder what is the best way to measure the execution time of some code in C++. Is there any built in Stopwatch (.Net) alike class? I am developing C++ on VS2010. How (if) can i use the .Net libraries inside my C++ project?? Thank you In advance.

like image 840
tropicana Avatar asked Feb 06 '12 14:02

tropicana


People also ask

What is Stopwatch C#?

Stopwatch is a class in C# to measure the elapsed time. Use it to calculate the time a function took to execute. It is found under System. Diagnostics.

How accurate is the Stopwatch class C#?

Stopwatch class does accurately measure time elapsed, but the way that the ElapsedTicks method works has led some people to the conclusion that it is not accurate, when they really just have a logic error in their code.

Is Stopwatch thread safe C#?

Looking at the source code, it is not thread-safe.


3 Answers

AFAIK C++ hasn't got an standard class like the Stopwatch in .NET.

http://cplus.about.com/od/howtodothingsi2/a/timing.htm is an example for a high resolution timer on the windows platform.

A platform independent implementation for such timers is: http://www.boost.org/libs/timer/doc/index.html

HTH

like image 96
paiden Avatar answered Sep 18 '22 22:09

paiden


You might consider http://code.google.com/p/cpp-stopwatch, it's in plain C++, no dependencies and comes with a handy Visual Studio solution. Oh, and I wrote it.

like image 23
tunnuz Avatar answered Sep 17 '22 22:09

tunnuz


You can use QueryPerformanceCounter to get a better timing when "profiling" some code (it's not perfect, but should be enough to get you starter).

BOOL WINAPI QueryPerformanceCounter( __out  LARGE_INTEGER *lpPerformanceCount );

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx.

like image 33
Max Avatar answered Sep 18 '22 22:09

Max