UtcNow
If you compare dates in your site without showing those dates to the visitor, DateTime.UtcNow
is a lot faster than DateTime.Now
.
Take this code, which compares against DateTime.Now
1000 times:
DateTime dt = DateTime.Now; for (int i = 0; i < 1000; i++) { DateTime dt2 = DateTime.Now; bool b = (dt2 > dt); }
The equivalent using UtcNow
is as follows:
DateTime dtu = DateTime.UtcNow; for (int i = 0; i < 1000; i++) { DateTime dtu2 = DateTime.UtcNow; bool b = (dtu2 > dtu); }
Here is how they compare on my machine:
Test |
Time taken (ticks) |
---|---|
1000 * Now |
3964 |
1000 * UtcNow |
136 |
Get ASP.NET Site Performance Secrets now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.