Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Stopwatch - performance penalty [duplicate]

Tags:

c#

.net

stopwatch

Possible Duplicates:
Is DateTime.Now the best way to measure a function's performance?
Stopwatch vs. using System.DateTime.Now for timing events

I have code which needs to run as fast as possible. To be able to log the execution time, I use the Stopwatch class. I suspect, Stopwatch may effect the performance in a bad way. Maybe using a DateTime difference may be more effective?

Which one do you think has better performance?

Stopwatch sw = new Stopwatch(); sw.Start(); int a = 5;  // Critical lines of code  long elapsedMs = se.Elapsed.TotalMilliseconds; 

OR

DateTime startDate = DateTime.Now; int a = 5;  // Critical lines of code  long elapsedMs = DateTime.Now.Subtract(startDate).TotalMilleseconds; 
like image 663
Ahmet Altun Avatar asked Aug 08 '11 17:08

Ahmet Altun


People also ask

How accurate is the stopwatch class C#?

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.

Do I need to stop stopwatch C#?

No, there is no need to stop or clean it up. Stopwatch does not use any unmanaged resources (if you thought of IDisposable ). It actually does not use any resources at all (except the memory used by the object itself, of course)! It also does not consume any CPU while measuring the elapsed time!

Is stopwatch thread safe C#?

Looking at the source code, it is thread-safe, but you must not use: Stop() , Reset() and Restart() .

How stopwatch is implemented in C#?

If you want to see how long a function takes to execute, add stopWatch. Start() before and stopWatch. Stop() after the function calls. You can now see total time it took(in seconds/milliseconds etc) using stopWatch.


2 Answers

The Stopwatch isn't doing anything between the calls to Start and Stop... It just stores the current timestamp (via QueryPerformanceCounter) when you start it, and compare it to the current timestamp when you stop it. So there is no reason it could affect the performance of your code, at least not significantly. Stopwatch was designed specifically for accurate time measurements, so you can be sure it is thoroughly optimized. It is also much more accurate than comparing successive values of DateTime.Now. ...

like image 127
Thomas Levesque Avatar answered Oct 12 '22 22:10

Thomas Levesque


Since your profiling code gets executed only once, its performance impact should be negligible. It's only a problem if you put calls to stopwatch inside your inner loop/critical codepaths.

GetTickCount() should be one of the fastest ways to profile, but it only has an accuracy of a few milliseconds. The GetTickCount() Windows API function does only check a simple variable (which is updated every few milliseconds); its cost is the cost of a native method invocation and nothing more. It's exposed as Environment.TickCount in .NET. But as I said, I doubt this matters. DateTime.UtcNow/Now have the same (low) accuracy as GetTickCount.

In theory, there could be some effect on the jitter, but that's unlikely.

like image 45
CodesInChaos Avatar answered Oct 12 '22 22:10

CodesInChaos