473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Performance Hit for String.IsNullOrEmpty()?

In spite of the obvious advantage of not encountering a
NullReferenceException unexpectedly in your running program is there
an offset cost in performance for using String.IsNullOrEmpty() over !=
""? If so, would it be of any great significance if a large number of
strings were being tested over the run of an application?

Ken Wilson
Seeking viable employment in Victoria, BC
Nov 17 '05 #1
8 7351
Ken Wilson <kw*********@NOshaw.SPAMca> wrote:
In spite of the obvious advantage of not encountering a
NullReferenceException unexpectedly in your running program is there
an offset cost in performance for using String.IsNullOrEmpty() over !=
""? If so, would it be of any great significance if a large number of
strings were being tested over the run of an application?


Performance should always be secondary to correct behaviour. If a
string can be null and that needs to be checked, you should use
IsNullOrEmpty (or the manual pair of checks on 1.1). If you don't need
to check for null, just use != "". As the behaviour is significantly
different, you shouldn't be worrying about the performance, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ken Wilson <kw*********@NOshaw.SPAMca> wrote:
In spite of the obvious advantage of not encountering a
NullReferenceException unexpectedly in your running program is there
an offset cost in performance for using String.IsNullOrEmpty() over !=
""? If so, would it be of any great significance if a large number of
strings were being tested over the run of an application?


Performance should always be secondary to correct behaviour. If a
string can be null and that needs to be checked, you should use
IsNullOrEmpty (or the manual pair of checks on 1.1). If you don't need
to check for null, just use != "". As the behaviour is significantly
different, you shouldn't be worrying about the performance, IMO.


I have a little side question.

What has best performance:

String helloString="hello";
if(myString==helloString)
{
}

or

String helloString="hello";
if(myString.equals(helloString))
{
}

Best regards
Søren Reinke
Nov 17 '05 #3
Søren Reinke wrote:
I have a little side question.

What has best performance:

String helloString="hello";
if(myString==helloString)
{
}

or

String helloString="hello";
if(myString.equals(helloString))
{
}


(I assume you meant to write .Equals, not .equals.)

Well, again the semantics are different - the second will throw an
exception if myString is null, the first won't.

The performance of the second is *slightly* better when the strings are
short - as the strings increase in length, that difference becomes
completely insignificant. I'd be very surprised to find a real app
where the difference is *ever* significant.

However, the readability difference *is* significant, I believe - I
think the first version is much more readable (when you don't mind the
difference in semantics, eg because you know that myString is
non-null). Readability is almost always more important than
performance.

Jon

Nov 17 '05 #4
I did some profiling assuming s1 is not null, because I thought it was
exactly the same.
You were right Jon and I don't understand why s1==s2 is slower than
s1.Equals(s2), and much slower when the strings are short.

Do you know why it can reach 75% of the speed ?

Whatever is the answer, s1==s2 is more readable and you dont have to check
if s1 is null.

"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de
news:11**********************@g44g2000cwa.googlegr oups.com...
Søren Reinke wrote:
I have a little side question.

What has best performance:

String helloString="hello";
if(myString==helloString)
{
}

or

String helloString="hello";
if(myString.equals(helloString))
{
}


(I assume you meant to write .Equals, not .equals.)

Well, again the semantics are different - the second will throw an
exception if myString is null, the first won't.

The performance of the second is *slightly* better when the strings are
short - as the strings increase in length, that difference becomes
completely insignificant. I'd be very surprised to find a real app
where the difference is *ever* significant.

However, the readability difference *is* significant, I believe - I
think the first version is much more readable (when you don't mind the
difference in semantics, eg because you know that myString is
non-null). Readability is almost always more important than
performance.

Jon
Nov 17 '05 #5
Ludovic SOEUR wrote:
I did some profiling assuming s1 is not null, because I thought it was
exactly the same.
You were right Jon and I don't understand why s1==s2 is slower than
s1.Equals(s2), and much slower when the strings are short.
s1==s2 has to check for nullity in the operator.
s1.Equals(s2) make a virtual call, which makes the CLR generate code to
do the nullity check anyway. My guess is that that code is faster than
a straight "if (x==null)" kind of check. It's only a guess though.
Do you know why it can reach 75% of the speed ?

Whatever is the answer, s1==s2 is more readable and you dont have to check
if s1 is null.


Exactly - assuming that the behaviour you want is to get true when both
of them are null and false when s1 is null but s2 isn't, s1==s2 is the
best you can get, IMO.

Jon

Nov 17 '05 #6

Thanks Jon and Ludovic for the answers.

Best regards
Søren

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Ludovic SOEUR wrote:
I did some profiling assuming s1 is not null, because I thought it was
exactly the same.
You were right Jon and I don't understand why s1==s2 is slower than
s1.Equals(s2), and much slower when the strings are short.


s1==s2 has to check for nullity in the operator.
s1.Equals(s2) make a virtual call, which makes the CLR generate code to
do the nullity check anyway. My guess is that that code is faster than
a straight "if (x==null)" kind of check. It's only a guess though.
Do you know why it can reach 75% of the speed ?

Whatever is the answer, s1==s2 is more readable and you dont have to
check
if s1 is null.


Exactly - assuming that the behaviour you want is to get true when both
of them are null and false when s1 is null but s2 isn't, s1==s2 is the
best you can get, IMO.

Jon

Nov 17 '05 #7

"Jon Skeet [C# MVP]" <sk***@pobox.com> a écrit dans le message de
news:11**********************@g49g2000cwa.googlegr oups.com...
s1==s2 has to check for nullity in the operator.
s1.Equals(s2) make a virtual call, which makes the CLR generate code to
do the nullity check anyway. My guess is that that code is faster than
a straight "if (x==null)" kind of check. It's only a guess though.


I decompiled String class.

op_Equality(String, String) wich is what is called with s1==s2, this is the
code that is executed :
public static bool operator ==(string a, string b)
{
return string.Equals(a, b);
}

So let's have a look to String.Equals(string a, string b) :
public static bool Equals(string a, string b)
{
if ((object)a == (object)b) return true;
if ((a != null) && (b != null)) return a.Equals(b);
return false;
}

There is first a test that check if the two strings are the same object in
order not to do a useless comparison
Then, it calls s1.Equals(s2) only if none of s1 and s2 are null.

To conclude, s1==s2 make a call to a function that checks if the objects are
the same, then checks if none are null and then calls s1.Equals(s2);

That's why s1.Equals(s2) is "faster" than s1==s2.

To answer to Jon's guess, I decompiled a code with a 'null' check before
calling s1.Equals(s2);.....it's slower than the code in String.Equals(string
a, string b). Funny isn't it ? Maybe it's because it is not optimised by the
compiler. It doesn't keep in mind the addresses of s1 and s2 so it has to
"load" them twice in the registers.
Hope all of this helps,

Ludovic SOEUR.


Nov 17 '05 #8
On Fri, 28 Oct 2005 07:56:29 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Ken Wilson <kw*********@NOshaw.SPAMca> wrote:
In spite of the obvious advantage of not encountering a
NullReferenceException unexpectedly in your running program is there
an offset cost in performance for using String.IsNullOrEmpty() over !=
""? If so, would it be of any great significance if a large number of
strings were being tested over the run of an application?


Performance should always be secondary to correct behaviour. If a
string can be null and that needs to be checked, you should use
IsNullOrEmpty (or the manual pair of checks on 1.1). If you don't need
to check for null, just use != "". As the behaviour is significantly
different, you shouldn't be worrying about the performance, IMO.


Thanks for your reply. It makes perfect sense to use the appropriate
functionality for the situation you are expecting to deal with. I
imagine I can always do some timing tests should I run into a
situation where evaluating two strings is going to be a major part of
the task, given the caveat you've stated re: the possibility that null
strings may exist in the data being examined.

Ken Wilson
Seeking viable employment in Victoria, BC
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jacob | last post by:
Is there any way (maybe using an attribute) that I can force strings or other reference parameters to always have an instance even if they are blank? Ex: public string DoSomething(string Text)...
18
by: mrshrinkray | last post by:
Are there any know bugs with the ListView in .NET 2? I'm having problems with an application that takes 15 seconds in 1.1, and now takes over a minute. The code in question uses: listViewItem =...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
6
by: anonieko | last post by:
string.IsNullOrEmpty is a very flexible function but watch out sometimes you really mean to test not just for 'empty' strings but also 'blank strings'. In other words, if you use...
2
by: shapper | last post by:
Hello, How can I check if a parameter, for example "name", is available in a QueryString? I need to create a default value if the parameter is not available. Thanks, Miguel
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
2
by: shapper | last post by:
Hello, I need to check if a string is empty. Should I use: String.IsNullOrEmpty(MyString) or MyString Is Nothing? What is the difference?
42
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and...
2
by: SyGC | last post by:
Hey All, im trying to simply display an image depending on whether a text box has been populated or not. To do this im using the String.IsNullOrEmpty(My String) approach. Becuase the image...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.