Tutorials
Videos
Article
Codes
Tips
Forums
Interview
Notes
Blogs
Use DateTime.UtcNow instead of DateTime.Now for better performance (CSharp/C# code)
Always tries to use DateTime.UtcNow instead of DateTime.Now especially when you are using it inside for, foreach or while loop. Because DateTime.Now is slower and if using inside a loop it will show down your pc. The DateTime.UtcNow will give you the universal time which is much faster.
Please see the implementation details of DateTime.Now: (using decompiler)
public static DateTime Now
{
get
{
DateTime utcNow = UtcNow;
bool isAmbiguousLocalDst = false;
long ticks = TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(utcNow, out isAmbiguousLocalDst).Ticks;
long num2 = utcNow.Ticks + ticks;
if (num2 > 0x2bca2875f4373fffL)
{
return new DateTime(0x2bca2875f4373fffL, DateTimeKind.Local);
}
if (num2 < 0L)
{
return new DateTime(0L, DateTimeKind.Local);
}
return new DateTime(num2, DateTimeKind.Local, isAmbiguousLocalDst);
}
}
You can see above that there is a lot of stuff going on there just to return local time. By contrast, lets have a look at DateTime.UtcNow:
// This is approximatly 120 times faster than DateTime.Now above
public static DateTime UtcNow
{
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries"), SecuritySafeCritical]
get
{
return new DateTime((ulong) ((GetSystemTimeAsFileTime() + 0x701ce1722770000L) | 0x4000000000000000L));
}
}
Recently Viewed Tips
Visual studio reference specific version true or false?
Tips to avoiding common certification failures
Object reference not set to an instance of an object - Error
Use of web.config inside views folder in MVC applications
When you want to .testsettings file in Visual Studio 2012
Understand .runsettings and .testsettings files
SQL Server Best Practices
Use of .TestSettings file in Visual Studio 2012 (and 2010) solution
Combine all your javascripts and minify them
To check CSS3 transition end event in different browser
The HTML5 Figure Element
New HTML5 doc type, Simple and Easy
Difference between Javascript and JScript
Difference between !== and != in Javascript
Difference between === and == in Javascript
Globalizing the application
How to know div is hidden in JQuery?
Silverlight for Windows Phone Toolkit for ?Mango? : Getting Started
How to use caching in window or normal C# project.
Use Cookies in Asp.Net application important Tip
Using destructor in C#
Adding new line to a text file in C# .Net
Converting XML to String in Client/Server C# app
Create and use Extension Methods in C#
Disable Request Validation ASP.NET MVC
Advantage and Disadvantage of cookies in ASP.Net
Common coding standards used in c# projects?
Tips to use String Builder
Video Player and Photo Viewer for Facebook
Tip to compress Session Data in ASP.Net 4.0
Tips to improve your C# application performance in case you are using loops
access modifier for default interface
Difference between readonly and enabled property of textbox
Tip to include empty directory when package a web application
Tip to improve reference efficiency in JavaScript files inside a web project
Difference between the CurrentCulture property and the CurrentUICulture property?
Domain integrity,entity integrity and Referential intergrity
When do you absolutely have to declare a class as abstract
Tip to combine two tables Data in SQL Server using UNION
Tip to create and test Store Procedure Which Takes input Parameter
What is the advantage of hosting a WCF Service in IIS over Self Hosting
Detect browser type from jQuery
When to use #Temp and when @Temp
Tip to use Domain / AD authentication in ASP.Net 3.5 application
Tip to use web.config to save SMTP email locally instead of sending
Tips to use MessageBox in Silverlight
Tip to ensure a single instance of your application running on a machine
Tip to read XML (get value from xml) in Silverlight
Tip to use System.IO.Path to get file, directory or extension
Difference between Implicit interface implementation" and "Explicit interface implementation" in C#
Post Article
Post Code
Post Tip
Start Forum
Quick note for developers
Title :
Your Name :
Submit
Tweet
Follow
Follow