string comparison Vs DateTime comparison

Posted on 2012-09-04
Last Modified: 2012-09-05

Which is quicker

1. comparing 2 string variables
2. comparing 2 datetime variables.

How can I check that.
Question by:KaranGupta
    LVL 77

    Expert Comment

    by:David Johnson, CD, MVP
    I used powershell to do  the timing and comparing datetime is faster (uses .net libraries)

    $var1 = get-date
    $var2 = Get-Date 
    $var3 = "2012-12-01 12:01AM"
    $var4 = "2012-12-02 12:02AM"
    Measure-Command {if ($var1 -ne $var2) {}}
    Measure-Command {if ($var3 -ne $var4) {}}

    Open in new window

    1690 tics vs 2623 ticks

    Author Comment


    Can you please post .net code because I am not aware of powershell technology? Can you also tell how you have measured these ticks?

    Karan Gupta
    LVL 35

    Accepted Solution

    Comparing 2 string variables is quicker than date times.
    The time difference will depend on how different the strings are and the CPU power. In my case is seconds then in my PC is 10 to 7.
    I measured the ticks using the StopWatch class, you can do your own test using the code below:
    private void button2_Click(object sender, EventArgs e)
                Stopwatch checkTime = new Stopwatch();
                DateTime dt1 = DateTime.Now;
                DateTime dt2 = DateTime.Now.AddSeconds(5);
                bool compare1 = dt1 == dt2;
                Debug.WriteLine("Time dt= " + checkTime.ElapsedTicks);
                string s1 = dt1.ToString();
                string s2 = dt2.ToString();
                bool compare2 = s1 == s2;
                Debug.WriteLine("Time dt= " + checkTime.ElapsedTicks);

    Open in new window


    Author Closing Comment

    That is what I want. Thanks a lot

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now