4

I want to check whether or not a variable string data contain empty string.

Which is more efficient, data.Length==0 or data==string.Empty?

I forgot to say, the data has been checked and it is guaranteed to be not null.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165

9 Answers9

13

Test results for 100 million iterations:

Equality operator ==:   796 ms 
string.Equals:          811 ms 
string.IsNullOrEmpty:   312 ms 
Length:                 140 ms  [fastest]
Instance Equals:       1077 ms 

source

Peter
  • 27,590
  • 8
  • 64
  • 84
  • I guessed that `Length` will be the fastest and I was right:) Nice link, btw:) – Petar Minchev Jul 15 '11 at 12:22
  • That seems extremely slow for any of the examples, I wonder if those tests were accomplished with a Pentium I processor. – Jeff LaFay Jul 15 '11 at 12:23
  • @jlafay - Quote from the site - `I ran these if statements through 100 million iterations each. ` – Petar Minchev Jul 15 '11 at 12:25
  • @peer: how reliable is this result? – Second Person Shooter Jul 15 '11 at 12:27
  • For 100 million iterations, it seems ok. Length will just access a int in the string class. That is why it is so fast. Which operation you use, depends on what you want to accomplish. – Peter Jul 15 '11 at 12:28
  • @peer: This is just one test result. If you're doubtful, run your own tests. – BoltClock Jul 15 '11 at 12:37
  • @Petar, ok gotcha. I apparent glossed over that statement :) That makes a LOT more sense! I wonder if the OP will be doing 100 million iterations as well. – Jeff LaFay Jul 15 '11 at 12:39
  • 5
    Btw, I would prefer string.IsNullOrEmpty() over anything. It may be the second slowest (not too shabby) but it is the safest when considering bugs. – Jeff LaFay Jul 15 '11 at 12:41
  • Remember that when you're checking the length, the string **can still be `null`**. You need to explicitly check for that first. That's why `IsNullOrEmpty` is probably a better choice, at least with readability in mind. Not to mention once you get down into this level of micro-benchmarking, a `null` check will not be completely insignificant. – Cody Gray - on strike Jul 15 '11 at 13:07
  • I'm also quite leery of "performance" tests where it is not explicitly specified that the code was compiled *and* run with optimizations enabled... – Cody Gray - on strike Jul 15 '11 at 13:09
  • I really believe that if you were using `string.Empty` and not `""`, the times would be faster. With `""` it creates new instance of string every iteration. – Oleg Grishko Jan 22 '12 at 10:09
  • @peer: How did you make the test? I am interested to do it for other cases. Thank you in advance. – kiss my armpit Mar 10 '13 at 13:15
11

Use String.IsNullOrEmpty(data) rather

Rahul
  • 76,197
  • 13
  • 71
  • 125
2

I will choose the third one, it is less bug prone:

String.IsNullOrEmpty(data)

oleksii
  • 35,458
  • 16
  • 93
  • 163
2

I would say that you should use the String.isNullOrEmpty method to check for nulls as well.

Magnus Johansson
  • 28,010
  • 19
  • 106
  • 164
2

Neither check will be your bottleneck. However, if you opt for the first, you could run into a NullReferenceException if the string is null. You would not have that problem with the second.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
2

Logically, data.Length == 0 is more efficient because it is simply comparing two integer values, whereas data == String.Empty is comparing strings (although very short ones).

However, there are a number of optimizations that the compiler or framework can potential make to minimize or eliminate any difference. This makes it hard to make absolute statements without running your own timing tests.

In the end, I doubt the difference will be enough to notice.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
2

Best practice is to use String.IsNullOrEmpty (or, if it fits your requirements, from .Net 4.0 - String.IsNullOrWhiteSpace).

If you call s.Length then you will get a NullReferenceException if the string is null. This means you would need to check if(s == null || s.Length == 0). This will be the most effective and probably the quickest, but you might aswell use String.IsNullOrEmpty.

s == string.Empty would return false if the string was null (null is not the same as an empty string).

In terms of performance, don't spare any more time thinking about it. It will nearly never, never, never, never impact on performance.

David Neale
  • 16,498
  • 6
  • 59
  • 85
2

As people have mentioned, use string.IsNullOrEmpty(str), or string.IsNullOrWhiteSpace(str) introduced in the .NET 4.0 framework.

devdigital
  • 34,151
  • 9
  • 98
  • 120
1

Use String.IsNullOrEmpty() to check.

VMAtm
  • 27,943
  • 17
  • 79
  • 125