Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:30:56+00:00 2026-06-18T06:30:56+00:00

Recently I had one strange performance problem. I need to compare time intervals in

  • 0

Recently I had one strange performance problem.
I need to compare time intervals in cycle with large amount of iterations.
I used DateTime.TimeOfDay property to compare these intervals. However, I found that these comparisons are very slow versus DateTime comparisons. So, I had to create DateTime’s with 1 year 1 month and 1 day to speed up time’ intervals comparison’s.
I prepared a small example to show what I mean.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatesBenchmark
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            DateTime firstDate = DateTime.Now;
            DateTime secondDate = DateTime.Now.AddSeconds(5);
            for (int i = 0; i < 2000000; i++)
            {
                var a = firstDate.TimeOfDay > secondDate.TimeOfDay;
                //var a = firstDate > secondDate;
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
            Console.ReadKey();
        }
    }
}

I got 15ms (if first row in for cycle is commented) versus 176 ms (if second row in for cycle is commented) on my laptop.

My question is short. Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:30:57+00:00Added an answer on June 18, 2026 at 6:30 am

    Calling foo.TimeOfDay is doing this:

    public TimeSpan TimeOfDay
    {
        get
        {
            return new TimeSpan(this.InternalTicks % 864000000000L);
        }
    }
    

    By accessing the TimeOfDay property on 2 DateTime instances over 2 million iterations you are creating 4 million Timespan instances. However, that’s not the biggest expense.

    Digging further, you have:

    internal long InternalTicks
    {
        get
        {
            return (long)(this.dateData & 4611686018427387903uL);
        }
    }
    

    So you have 4 million instantiations, remainder calculations, casts, and & operations. These are all cheap operations (“cheap” of course being a relative term) but done in quantity they add up.

    The actual comparison is trivial:

    public static bool operator >(TimeSpan t1, TimeSpan t2)
    {
        return t1._ticks > t2._ticks;
    }
    

    Compiling the OPs code in Debug mode, I see:

    1. Empty loop: 4ms.
    2. var a = firstDate > secondDate; 6ms (suggesting it is not optimized away)
    3. var a = firstDate.TimeOfDay; 40ms
    4. var a = firstDate.TimeOfDay > secondDate.TimeOfDay; 80ms
    5. TimeSpan a = new TimeSpan( ticks ), b = new TimeSpan( ticks ); 7ms

    Running a performance analysis in VS 2012 against the program, 81% of samples are coming from DateTime.get_TimeOfDay().

    Running x64, Release mode, all optimizations enabled:

    1. Empty loop: 3ms.
    2. var a = firstDate > secondDate; 6ms
    3. var a = firstDate.TimeOfDay; 20ms
    4. var a = firstDate.TimeOfDay > secondDate.TimeOfDay; 40ms
    5. TimeSpan a = new TimeSpan( ticks ), b = new TimeSpan( ticks ); 6ms

    So:

    • Micro-benchmarks can be misleading (though not useless).
    • Enable optimizations before deciding there is a problem.
    • Performance appears to double with optimization.
    • The statements in question do not appear to be optimized away under any circumstance.
    • Instantiation is a tangible but minor part of the expense.
    • Cast/arithmetic operations account for the rest of the expense.
    • Storing the property value in a variable before looping greatly improves performance.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently had a problem when comparing two NSURLs and compare one NSURL with
I had this problem a long time ago and recently I got some clues
We recently had a code review . One of my classes was used so
We recently had one of our JVM's crash, leaving behind a core dump file
I've had a few questions about MEF recently, but here's the big one --
I recently had a similar thread about this, but now I need to animate
I've come across a strange issue in a custom Android build recently? I've had
I recently had the following problem: I'm developing a numerical library in Python (called
I recently had to update one of my model's properties from type StringProperty to
I recently had an interview question where I had to implement memcpy. I've used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.