Articles → .NET → String Comparison Vs Date Comparison Performance

String Comparison Vs Date Comparison Performance






Software Requirement





Prerequisite Knowledge





Project Creation




Picture showing the project structure in the solution explorer
Click to Enlarge


Code To Compare Performance


using System;
using System.Diagnostics;

namespace string_comparison_vs_datetime_comparison {
  class Program {
    static void Main(string[] args) {
      Stopwatch sw = new Stopwatch();
      DateTime dateOne = DateTime.Now;
      DateTime dateTwo = DateTime.Now.AddSeconds(5);
      sw.Start(); // Stop watch Starts
      bool compare1 = dateOne == dateTwo; // Comparing 2 date variables
      sw.Stop(); // Stop watch stops
      Console.WriteLine(string.Format("Time taken when comparing date variable {0}", sw.ElapsedTicks));
      string stringOne = dateOne.ToString();
      string stringTwo = dateTwo.ToString();
      sw.Reset(); //  Reset stop watch 
      sw.Start(); // Start again from zero
      bool compare2 = stringOne == stringTwo; // Comparing 2 string variables
      sw.Stop(); // Stop watch stops
      Console.WriteLine(string.Format("Time taken when comparing string variable {0} ", sw.ElapsedTicks));
      Console.Read();
    }
  }
}



Output


Picture showing the output of string comparison Vs date comparison performance
Click to Enlarge


Conclusion





Posted By  -  Karan Gupta
 
Posted On  -  Friday, September 7, 2012

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250