Nikodemus RIP
Nikodemus RIP

Reputation: 1379

Best performance comparing to string.Empty (c#)

Is there any difference between

if(string.Empty.Equals(text))

and

if(text.Equals(string.Empty))

concerning performance, unexpected behaviour or readability?

Upvotes: 2

Views: 736

Answers (5)

James L
James L

Reputation: 16864

They are the equivalent.

First case:

IL_0000: nop
IL_0001: ldsfld string [mscorlib]System.String::Empty
IL_0006: ldarg.0
IL_0007: callvirt instance bool [mscorlib]System.String::Equals(string)
IL_000c: ldc.i4.0
IL_000d: ceq
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: brtrue.s <target>

Second case:

IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldsfld string [mscorlib]System.String::Empty
IL_0007: callvirt instance bool [mscorlib]System.String::Equals(string)
IL_000c: ldc.i4.0
IL_000d: ceq
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: brtrue.s <target>

Upvotes: 4

ken2k
ken2k

Reputation: 48975

I would recommend using

if (string.IsNullOrEmpty(text))
{
}

as I think it's more readable, which is the most important here IMO (actually the result won't be the same for null values, but your second version would throw an exception for this particular test case).

I don't know if there is any difference in the generated IL, but in any case, such a micro-optimization (if any...) will clearly not make your application faster.


EDIT:

Just tested it as I'm curious:

private static void Main(string[] args)
{
    Stopwatch z1 = new Stopwatch();
    Stopwatch z2 = new Stopwatch();
    Stopwatch z3 = new Stopwatch();

    int count = 100000000;

    string text = "myTest";

    z1.Start();
    for (int i = 0; i < count; i++)
    {
        int tmp = 0;
        if (string.Empty.Equals(text))
        {
            tmp++;
        }
    }
    z1.Stop();

    z2.Start();
    for (int i = 0; i < count; i++)
    {
        int tmp = 0;
        if (text.Equals(string.Empty))
        {
            tmp++;
        }
    }
    z2.Stop();

    z3.Start();
    for (int i = 0; i < count; i++)
    {
        int tmp = 0;
        if (string.IsNullOrEmpty(text))
        {
            tmp++;
        }
    }
    z3.Stop();

    Console.WriteLine(string.Format("Method 1: {0}", z1.ElapsedMilliseconds));
    Console.WriteLine(string.Format("Method 2: {0}", z2.ElapsedMilliseconds));
    Console.WriteLine(string.Format("Method 3: {0}", z3.ElapsedMilliseconds));

    Console.ReadKey();
}

Not sure the test is relevant because testing micro-optimizations is always more complicated than it looks, but here are some results:

Method 1: 611
Method 2: 615
Method 3: 336

Method 1 and 2 are identical as expected, and method 3, the more readable solution IMO, looks like the fastest one, so make your choice ;)

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

Concerning performance they will behave the same. Concerning unexpected behavior, the second might throw a NullReferenceException if text = null. if (!string.IsNullOrEmpty(text)) seems more natural though to achieve the same thing (same performance, same result) without the undesired unexpected behavior and the possibility of a NRE.

Upvotes: 5

Daniel Mann
Daniel Mann

Reputation: 58980

There should be no performance difference in this case. It's like comparing "x==y" to "y==x".

I'd say that if (text == string.Empty) is the most readable syntax, but that's just me.

Of course, you could also use if (string.IsNullOrEmpty(text)) { }, or string.IsNullOrWhiteSpace(text).

As for unexpected behavior, it's a pretty straightforward string comparison. I can't imagine how you'd get unexpected behavior out of it.

Upvotes: 0

Oded
Oded

Reputation: 498934

These will have the same performance characteristics.

If text is null, your second line with throw a NullReferenceException.

Personally I find:

if(text == string.Empty)

More readable than either your options.

And there are the built in:

if(string.IsNullOrEmpty(text))

And for .NET 4.0:

if(string.IsNullOrWhitespace(text))

Upvotes: 5

Related Questions