• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 673
  • Last Modified:

string comparison Vs DateTime comparison

Hi

Which is quicker

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

How can I check that.
0
KaranGupta
Asked:
KaranGupta
  • 2
1 Solution
 
David Johnson, CD, MVPOwnerCommented:
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
0
 
KaranGuptaAuthor Commented:
Hi

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

Regards
Karan Gupta
0
 
Miguel OzSoftware EngineerCommented:
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);
            checkTime.Start();
            bool compare1 = dt1 == dt2;
            checkTime.Stop();
            Debug.WriteLine("Time dt= " + checkTime.ElapsedTicks);
            string s1 = dt1.ToString();
            string s2 = dt2.ToString();
            checkTime.Reset();
            checkTime.Start();
            bool compare2 = s1 == s2;
            checkTime.Stop();
            Debug.WriteLine("Time dt= " + checkTime.ElapsedTicks);
}

Open in new window

0
 
KaranGuptaAuthor Commented:
That is what I want. Thanks a lot
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now