@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

December 9, 2011 comment , ,

Summarizing DateTime.Now & StopWatch In C#

In last post, Efficient Use of Try Catch Block, we have used StopWatch class to measure CPU ticks consumed by particular code execution. After reading that one of the reader asked cant we use DateTime.Now instead. So here I have tried to explain the same.

Many times we have found developer to use DateTime.Now to measure the performance of code execution. But if you google it out, you will find plenty of source which recommend using StopWatch class instead DateTime.Now. Here I am summarizing few of them.

As per the MSDN

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time. Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation.

It means StopWatch class uses higher resolution / precision than DateTime.Now. So it is worth to use StopWatch class for more accurate result.

One more thing we need to consider while using DateTime.Now function is that DateTime.Now return Date and Time after applying system time zone and daylight saving time settings. So it also adds overhead to the DateTime.Now function.

In above output, we can see that DateTime.Now return time zone applied value (UTC +05:30 in my case). So one may recommend using DateTime.UtcNow to overcome time zone calculation overhead. But it will not also helpful. Continue reading what is difference between DateTime.Now and DateTime.UtcNow :)

Moreover StopWatch class is shipped under System.Diagnostics namespaces hence Microsoft designed StopWatch class keeping in mind that it would more useful for diagnosis purpose and provide utmost accurate result.

However as per Barfield’s comment on John Chapman’s blog that StopWatch uses more system resources. But I don’t think that you will consider it while you are benchmarking your system.

We have enough discussion. Now let see one small example which shows clear measurement difference discussed here.

Use of DateTime.Now

DateTime dtStart;
DateTime dtEnd;
TimeSpan tsDifference;
 
dtStart = DateTime.Now;
System.Threading.Thread.Sleep(500);
dtEnd = DateTime.Now;
 
tsDifference = dtEnd - dtStart;
 
Console.WriteLine("CPU Ticks Count With Now : " + tsDifference.Ticks);

Output:

Use of DateTime.UtcNow

DateTime dtStart;
DateTime dtEnd;
TimeSpan tsDifference;
 
dtStart = DateTime.UtcNow;
System.Threading.Thread.Sleep(500);
dtEnd = DateTime.UtcNow;
 
tsDifference = dtEnd - dtStart;
 
Console.WriteLine("CPU Ticks Count With UtcNow : " + tsDifference.Ticks);

Output:

Use of StopWatch

Stopwatch sw = new Stopwatch();
 
sw.Start();
System.Threading.Thread.Sleep(500);
sw.Stop();
 
Console.WriteLine("CPU Ticks Count With StopWatch : " + sw.ElapsedTicks);

Output:

Conclusion

Based on above discussion and example, we can clearly see that use of StopWatch is preferable over DateTime.Now and DateTime.UtcNow as Stopwatch provide more accurate result.

What do you say reader???

comments powered by Disqus

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading