String comparison is one most frequent we do while coding, especially, comparison with emtpy string.For a string TestString, We tend to do it in mainly 3 ways
1. if(TestString == "")
2. if(TestString == string.Empty)
3. if(TestlLength == 0)
DateTime time = DateTime.Now;
string TestString = "";
for(int i =0;i<10000000;i++)
{
if(TestString.Length == 0)
if(TestString == "")
{
}
}
double TimeTaken = (DateTime.Now-time).TotalMilliseconds;
Console.WriteLine(string.Format("Taken {0}", TimeTaken));
I wrote up a loop to check the performance and results were startling. The first two, ( string.empty and == ) took almost double the time string.length took. Of course it is understandle , while former would require comparison to each place in string, the latter only need to know the length.
The point i would like to make here is when you need to check string for empty, it is always better to use string.length if you have performance as your criteria.
Quick Connection to Exchange Online - o365
5 years ago
