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!!

#1  avatar Jeremy Pearson on 30 Jul 2004 at 14:17

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

#2 ian avatar Ian Nelson on 30 Jul 2004 at 15:43

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.

#3  avatar Jonh on 30 Jun 2006 at 09:35

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.

#4  avatar Jonh on 30 Jun 2006 at 09:43

Jez said:

"This probably won't hit your 50 million iterations, but may still impact on performance. "

It will not impact performance!

#5 ian avatar Ian Nelson on 02 Jul 2006 at 04:41

Jonh - no, it probably doesn't matter in the grand scheme of things, it's just academically interesting.

#6  avatar Olivia Vasquez on 19 Jul 2007 at 08:16

This also has some graphs to go with the numbers for the academical ones:

http://www.tbiro.com/Check-empty-string-performance.htm

O

 

#7  avatar ac on 18 Nov 2008 at 21:42

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.

#8  avatar Bahadır ARSLAN on 05 Mar 2009 at 06:56

What about

string.IsNullOrEmpty(strVar)

?

#9 ian avatar Ian Nelson on 05 Mar 2009 at 08:43

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

Leave a Comment