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.
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.
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.
Looking at the source code, it is not thread-safe.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With