DateTime.Now vs DateTime.UtcNow

A lot of times we used DateTime.Now for logging purposes. Once we got a task to search for and avoid performance bottlenecks in a data distributor projects. As a most simple measurement we put some logging around the code blocks in which we suspect the bottlenecks. The log showed us that a lot of time spent in a method which was really simple and we didnt understand why is it so slow.

After choosing professional performance profiler (RedGate’s ANTS Performance Profiler is the best one! 🙂 ) we found that not the method was so slow but the logging! More precisely the DateTime.Now calls while writing timestamp on log lines!

After some googling we found that DateTime.Now after determining UTC time asks the OS for the timezone set on machine and computes the local time from it. And the determination of timezone from the OS was the real bottleneck.

So in high performing solutions use DateTime.UtcNow instead of DateTime.Now if You dont want to run into things like this.

Leave a Reply

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