O'Reilly logo

ASP.NET Site Performance Secrets by Matt Perdeck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required