BLOG.CSHARPHELPER.COM: Determine the fastest way to see if a string is blank in C#
Determine the fastest way to see if a string is blank in C#
This example uses three strings to see which method for detecting blank strings is fastest. The strings are initialized by the following code.
string string1 = "", string2 = "ABCD", string3;
For the entered number of trials and for each method, the code tests string1 and string2 to see which is empty. The program checks both strings in case there's a difference in performance testing blank strings and non-blank strings.
The following list shows the four tests the program executes.
if (string1 == string.Empty) string3 = string1;
if (string2 == string.Empty) string3 = string2;
if (string1.CompareTo(string.Empty) == 0) string3 = string1;
if (string2.CompareTo(string.Empty) == 0) string3 = string2;
if (string1 == "") string3 = string1;
if (string2 == "") string3 = string2;
if (string1.Length == 0) string3 = string1;
if (string2.Length == 0) string3 = string2;
After several people suggested adding string.IsNullOrEmpty and string.IsNullOrWhiteSpace (see the comments below), I added those tests:
if (string.IsNullOrEmpty(string1)) string3 = string1;
if (string.IsNullOrEmpty(string2)) string3 = string2;
if (string.IsNullOrWhiteSpace(string1)) string3 = string1;
if (string.IsNullOrWhiteSpace(string2)) string3 = string2;
Note that string.IsNullOrWhiteSpace is new in the .NET Framework 4.5 so you won't be able to use it unless you have that version or later installed. I normally try to keep the versions I post downgraded so more people can use them so the example that's available for download doesn't use that version. Uncomment the code indicated in the program to use this method if you have .NET Framework 4.5 or later.
If you look closely at the picture you'll see that testing the string's Length property was fastest, using == to compare a string to string.Empty and "" took about twice as long, and surprisingly the CompareTo method took more than 25 times as long.
The string.IsNullOrEmpty method took slightly longer than checking whether Length == 0, which makes me think it may be smart enough to make that test itself.
In one test on my Windows 8 machine, IsNullOrWhiteSpace took surprisingly long, more than twice as long as == string.Empty.
Keep in mind that the tests were performed a huge number of times (100 million times in this test) so the total amount of time used is tiny. The CompareTo method is slow and isn't all that readable. The IsNullOrEmpty method is slightly more readable. IsNullOrWhiteSpace just plain takes too long. Testing the string's Length is fast but fairly obscure.
I usually use one of the == tests because they provide good readability and only slightly worse performance than checking the Length property.
11/1/2012 1:10 PM
Anonymous wrote: What about string.IsNullOrEmpty(string1); for handling potentially uninitialized strings? Reply to this
11/1/2012 1:14 PM
E Anderson wrote: I typically use string.IsNullOrEmpty(). Adding this to your test code shows it's a tad bit slower than (Length == 0):
string.Empty: 1.04 seconds
CompareTo(): 15.02 seconds
"": 1.01 seconds
Length == 0: 0.68 seconds
string.IsNullOrEmpty(): 0.89 seconds Reply to this
11/2/2012 4:53 PMRod Stephens wrote: After seeing all of these good suggestions about testing string.IsNullOrEmpty and string.IsNullOrWhiteSpace, I added them. See the revised test above. The bottom line is IsNullOrEmpty does okay but IsNullOrWhiteSpace does surprisingly poorly. Reply to this
What about string.IsNullOrEmpty(string1); for handling potentially uninitialized strings?
Reply to this
I typically use string.IsNullOrEmpty(). Adding this to your test code shows it's a tad bit slower than (Length == 0):
string.Empty: 1.04 seconds
CompareTo(): 15.02 seconds
"": 1.01 seconds
Length == 0: 0.68 seconds
string.IsNullOrEmpty(): 0.89 seconds
Reply to this
Very Clear Explanation. Good.
Reply to this
What about string.IsNullOrEmpty()? and string.IsNullOrWhiteSpace()?
Reply to this
After seeing all of these good suggestions about testing string.IsNullOrEmpty and string.IsNullOrWhiteSpace, I added them. See the revised test above. The bottom line is IsNullOrEmpty does okay but IsNullOrWhiteSpace does surprisingly poorly.
Reply to this