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

  • Home
  • SEARCH
  • 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 981719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:35:11+00:00 2026-05-16T04:35:11+00:00

Another unimportant performance question. Unimportant because mostly code-readability is much more important than a

  • 0

Another “unimportant” performance question.
Unimportant because mostly code-readability is much more important than a few milliseconds, but interesting anyway.
I noticed that there are differences between different DateTime Comparisons.

I checked 3 alternatives:

    Dim clock As New System.Diagnostics.Stopwatch
    Dim t1, t2, t3 As Long
    Dim Date1 As Date = Date.Now.AddSeconds(2), Date2 As Date = Date.Now
    Dim isGreaterThan As Boolean
    clock.Start()
    For i As Int32 = 1 To 1000000000
        isGreaterThan = Date1 > Date2
    Next
    clock.Stop()
    t1 = clock.ElapsedMilliseconds
    clock.Reset()
    clock.Start()
    For i As Int32 = 1 To 1000000000
        isGreaterThan = Date.Compare(Date1, Date2) > 0
    Next
    clock.Stop()
    t2 = clock.ElapsedMilliseconds
    clock.Reset()
    clock.Start()
    For i As Int32 = 1 To 1000000000
        isGreaterThan = Date1.CompareTo(Date2) > 0
    Next
    clock.Stop()
    t3 = clock.ElapsedMilliseconds

The results:

  1. Date1 > Date2 = 13207/13251/13267/13569/13100 = 13279 ms
  2. Date.Compare(Date1, Date2) > 0 = 13510/13194/13081/13353/13092 = 13246 ms
  3. Date1.CompareTo(Date2) > 0 = 11776/11768/11865/11776/11847 = 11806 ms

Normally i choose first method because it is more readable and fail-safer than the others.
Method 3 is the fastest. Is this the method that the compiler would choose when i use the operator overloaded method 1? When yes, why are there differences during runtime?
Method 2 is the slowest, maybe because it is shared/static?!
UPDATE: added some more values. Now Metod 1 and 2 are pretty equal fast.

Perhaps somebody could clarify it with facts.
Thanks.

  • 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-05-16T04:35:12+00:00Added an answer on May 16, 2026 at 4:35 am

    Let’s use some Reflector-Magic on this functions (all from the DateTime-Type):

    Operator ” > “

    public static bool operator >(DateTime t1, DateTime t2)
    {
        return (t1.InternalTicks > t2.InternalTicks);
    }
    
    private long InternalTicks
    {
        get
        {
            return (((long) this.dateData) & 0x3fffffffffffffffL);
        }
    }
    
    // this.dateData is a private field
    

    Compare

    public static int Compare(DateTime t1, DateTime t2)
    {
        long internalTicks = t1.InternalTicks;
        long num2 = t2.InternalTicks;
        if (internalTicks > num2)
        {
            return 1;
        }
        if (internalTicks < num2)
        {
            return -1;
        }
        return 0;
    }
    

    CompareTo

    public int CompareTo(DateTime value)
    {
        long internalTicks = value.InternalTicks;
        long num2 = this.InternalTicks;
        if (num2 > internalTicks)
        {
            return 1;
        }
        if (num2 < internalTicks)
        {
            return -1;
        }
        return 0;
    }
    

    Let’s break it down:

    Operator >

    • Two calls against a private property
    • Each call issues one cast and one binary And

    Compare and CompareTo

    • Creates two variables and compares them

    CompareTo is most likely faster because it does not need to access an outside object, but can instead draw one of the variables from itself.

    Edit: From looking at the functions I realize that the performance of > should always stay the same, while the performance of Compare and CompareTo are based on the passed values:

    • t1 is greater: fastest
    • t2 is greater: middle
    • t1 == t2: slowest

    Though, the performance gain or loss we’re talking about is…how shall I say…I couldn’t care less. 😉 But it’s quiet interesting, I agree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Another translation question, this may be more theoretical, but I am curious as to
Another simple question. I found this really cool snippet of code: $date_str = Jan
Another recursion question. What I've tried to develop for the past few hours is
Another question about UDID ... UDID is a unique identifier for the phone, but
Another question on SO inspired me to try this code in C#: class Program
Another SCJP question. I think the output is K=7, but the book's answer is
another newbie-question regarding rails and bootstrap. I am using something like this: <div class=tabbable
Another git question... I am in the following situation: A1 ---- B1 ---- C1
Another clipboard question: When text is put onto the clipboard, it frequently goes in
Another question related to this one . I have a List<SortableObjects> that is the

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.