Is my string empty? Some C# performance metrics
Update (25/02/2006) – Hey, we now have String.IsNullOrEmpty, you know!
What’s the quickest way, in C#, to test whether a string is empty? This is a question which came to my mind yesterday. I’ve always been fond of:
if (myString==String.Empty)
but it occured that this necessitates the creation and destruction of a second object (the empty string with which we’re comparing), so maybe it would be quicker to write:
if (myString.Length==0)
And looking through some existing code I also found examples of:
if String.Equals(myString,String.Empty)
I decided that an experiment was in order, so wrote a quick windows application to try each method in turn. I was also interested to see if there was a difference in performance where the result was true than when it was false.
Here are the results, with times in milliseconds for 50 million iterations:
| true result | false result | |
| myString==String.Empty | 1172 | 2484 |
| myString.Length==0 | 531 | 531 |
| String.Equals(myString,String.Empty) | 611 | 1893 |
So there you have it. Experimental evidence that the fastest way to test for an empty string is to test whether it’s Length property is zero (and that this also takes exactly the same length of time irrespective of whether the result is true or false).. unless anyone knows of a quicker method? Of course, since we’re only saving ourselves around 50 nanoseconds per iteration against using the slowest method shown above, this is hardly going to result in a noticeable performance gain!!









The Visual Basic 6 help files always recommended testing the string length to find empty strings, rather than comparing with an empty string literal, eg:
If Len(strMyString) = 0 Then
..rather than:
If strMyString = "" Then
..or (slightly better):
Const strEMPTY_STRING As String = ""
If strMyString = strEMPTY_STRING Then
I agree we’re splitting hairs over the timings, but imagine processing a very large text file or database output, which may have empty strings as fields. This probably won’t hit your 50 million iterations, but may still impact on performance.
Incidentally, referring back to the original C# code, could you not just say:
if (myString.Length)
..which, as it returns zero, returns false automatically (non-zero would be true), saving an ‘==0′ comparison? Just a thought, although as C# is strongly typed this may not actually work..
Jez
Hi Jez,
You’re right, due to C# being strongly-typed you’ll get a compile-time error if you tried
if (myString.Length)
- cannot implicitly convert type ‘int’ to ‘bool’. And of course if you tried casting to a boolean this would fail for those occasions when the Length were greater than 1.
Does it really matter?
What kind of application will suffer a significant impact while testing empty strings ? Even if the aplication has to compare 1,000,000 that came from a database or file the performance probably be IO bound.
Jez said:
"This probably won’t hit your 50 million iterations, but may still impact on performance. "
It will not impact performance!
Jonh – no, it probably doesn’t matter in the grand scheme of things, it’s just academically interesting.
This also has some graphs to go with the numbers for the academical ones:
http://www.tbiro.com/Check-empty-string-performance.htm
O
This might be a mut point for the apps you (and definitly I) work on, but i could see this maybe being relevant for a high traffic website for example.
What about
string.IsNullOrEmpty(strVar)
?
Bahadir,
Well, that wasn’t an option in July 2004 when I wrote this blog post!
But yes, if you want to check whether a string is null OR empty, it does seem to be the appropriate option to use for .NET 2.0 and upwards.
I blogged a little more about this method at ianfnelson.com/…/String_IsNullOr
Counts Length low ms high ms
test.Length == 0 1000000000 0 713
test.Length == 0 1000000000 100 730 739
test.Length == 0 1000000000 1000 732 740
test.Length == 0 1000000000 10000 720 721
test == "" 1000000000 0 663
test == "" 1000000000 100 891 900
test == "" 1000000000 1000 875 903
test == "" 1000000000 10000 895 900
test == string.Empty 1000000000 0 663
test == string.Empty 1000000000 100 892 900
string.IsNullOrEmpty 1000000000 1000 576 584
string.IsNullOrEmpty 1000000000 10000 584 586
so, string.ISNullOrEmpty wins at 584ms