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