Articles → .NET → string comparison Vs date comparison performance

string comparison Vs date comparison performance


















Software Requirement





Prerequisite knowledge





Project creation





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



Click to Enlarge


Conclusion





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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250