Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# DateTime Performance Class

This C# performance class optimizes accesses to the DateTime.Now method.

DateTime performance. A program uses DateTime.Now to frequently capture the time.

Can we optimize this? We use a custom class to improve DateTime performance. We cache the DateTime value for Now, improving performance significantly at the cost of accuracy.

Getting current time performance

DateTimeNowCache:  16.5 ns
DateTime.Now:     264.8 ns

Example. Here we see a static utility class in C# that you can use to throttle the number of times the system is queried for the current DateTime. In the class, the constant 20 is used, which means the DateTime is only updated 5% of the time.

ConstStatic Class

C# program that caches DateTime instances

using System;

/// <summary>
/// Utility class to return new DateTimes every few requests.
/// </summary>
public static class DateTimeNowCache
    /// <summary>
    /// Refresh time after this many requests.
    /// </summary>
    const int _count = 20;

    /// <summary>
    /// The most recent time collected.
    /// </summary>
    static DateTime _recentTime = DateTime.Now;

    /// <summary>
    /// Number of skipped time collections
    /// </summary>
    static int _skipped;

    /// <summary>
    /// Get the DateTime within the last N calls.
    /// </summary>
    /// <returns>Recent DateTime collected.</returns>
    public static DateTime GetDateTime()
	if (_skipped > _count)
	    _recentTime = DateTime.Now;
	    _skipped = 0;
	return _recentTime;

The class uses internal accounting to "throttle" the number of times the DateTime is updated. Therefore, using GetDateTime() will be around 20x faster than DateTime.Now, but less "current" 95% of the time.

Benchmark. If we inspect DateTime.Now in IL Disassembler, we see that it internally calls many methods and probably executes hundreds or more lines of code. Eventually, DateTime.Now queries the Windows operating system.

Info: System calls like this are often slow compared to other managed code. We use the above DateTimeNowCache code in a tight loop.

IL Disassembler TutorialIntermediate Language

Notes on benchmark

Iterations: 10000000

Code compared

DateTime now = DateTimeNowCache.GetDateTime();
DateTime now = DateTime.Now;

Benchmark results. Here, the DateTime cache code is about 94% faster. This indicates that the time required for the logic inside the DateTimeNowCache class is minuscule compared to a system query.

Note: Where you would use DateTime.Now as the value you are getting, you can instead use DateTimeNowCache.GetDateTime().

Use. The code here is useful in applications that frequently query the time and service many requests in small amounts of time. For example, if you record traffic on your website, you can use this to improve performance.

Note: The times reported may be a few seconds outdated. Do not use it if the time is critical to your application.

Alternative. An alternative approach is possible. A static DateTime field exists. A static Timer instance updates the DateTime every several seconds. Code that needs the current time simply accesses the static DateTime field.


Summary. We avoided recalculating the time and repeatedly querying the operating system. In some applications, this can measurably improve performance, as it reduces the number of interop calls to the OS. We cached the DateTimes.

Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf