System.Diagnostics.Stopwatch is no longer my friend….

So..I have been thinking for quite some time, “Gee, wouldn’t it be nice to have a nice and easy to use timer class built right into the framework?”. I decided to go and use the lovely Stopwatch class to start building a lot of timing routines into my applications for debugging and logging purposes. The syntax was so easy and beautiful.

var sw = new System.Diagnostics.Stopwatch()

Can’t get much simpler than that, right? Well…..that is if the stopwatch returned reliable results….

It seems on one our computers we run this code and a 23 second process returns 10.5 seconds…Yeah, same thing I thought.. What a bunch of crap!

So, I read something about Processor Affinity and CPU clock stepping that may be causing the issue. Either way, I don’t want to have to set the threads processor affinity or try and manage the CPU’s frequency just to take a simple time measurement……Back to the old

DateTime start = DateTime.Now();
TimeSpan elapsed = DateTime.Now() - start;

I know…not much difference, but didn’t the Stopwatch look so much cooler?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.