Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Performance / March 2008

Tip: Looking for answers? Try searching our database.

Better coding

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jsh02_nova - 14 Mar 2008 04:04 GMT
Which code is really better in term of performance, A or B?
A) list_of_strings[0] == ""
B) list_of_strings[0].Length.Equals(0)
Mufaka - 14 Mar 2008 04:18 GMT
> Which code is really better in term of performance, A or B?
> A) list_of_strings[0] == ""
> B) list_of_strings[0].Length.Equals(0)

The difference is negligible. Do whatever is most readable or follows
convention for your team. In theory, checking the length should be
quicker, but some say that checking against String.Empty is quicker too.

If you are really at that point in optimizing your code, I am jealous :P
Aidy - 14 Mar 2008 10:59 GMT
> Which code is really better in term of performance, A or B?
> A) list_of_strings[0] == ""
> B) list_of_strings[0].Length.Equals(0)

C) list_of_strings[0].Length == 0

:)

A string holds its length as part of its structure which is why quering the
length carries no overhead, unlike comparing it to another string.  However
as has been said, comparing two empty strings probably isn't any slower in
reality.
Jon Skeet [C# MVP] - 14 Mar 2008 11:21 GMT
> > Which code is really better in term of performance, A or B?
> > A) list_of_strings[0] == ""
[quoted text clipped - 8 lines]
> as has been said, comparing two empty strings probably isn't any slower in
> reality.

Especially as the first thing a string equality comparison is likely to
do is compare the lengths.

Personally I find
if (x=="")
easier to read - it conveys the intention of "is x an empty string"
better (to me) than
if (x.Length==0)

Very much a personal preference thing - but I'd make the choice based
on readability rather than performance.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Aidy - 14 Mar 2008 12:29 GMT
> Especially as the first thing a string equality comparison is likely to
> do is compare the lengths.
[quoted text clipped - 7 lines]
> Very much a personal preference thing - but I'd make the choice based
> on readability rather than performance.

I see where you're coming from, but I haven't seen the "==" op code so don't
know if it has a special case for comparing empty strings.  I don't know if
it says "both strings are 0 length so return true" or if it initiates a
"foreach" loop anyway.  Even if it compares both string lengths to see if
they are 0 and return true, that is still two comparisons.  Whereas I know
that just checking if the length is 0 is only one comparison.

Another reason I dislike comparison with an empty string is that some people
(read: other developers) don't appreciate that you get the odd stray spaces
and that "" != " ".  The fact that you are checking a zero length string
makes it more obvious (to me anyway) that rogue spaces are not to be
tolerated.
Jon Skeet [C# MVP] - 14 Mar 2008 13:04 GMT
> > Very much a personal preference thing - but I'd make the choice based
> > on readability rather than performance.
[quoted text clipped - 5 lines]
> they are 0 and return true, that is still two comparisons.  Whereas I know
> that just checking if the length is 0 is only one comparison.

Show me *any* production application where that makes any significant
difference, and I'll buy you several pints. It's really not worth
bothering about.

> Another reason I dislike comparison with an empty string is that some people
> (read: other developers) don't appreciate that you get the odd stray spaces
> and that "" != " ".  The fact that you are checking a zero length string
> makes it more obvious (to me anyway) that rogue spaces are not to be
> tolerated.

Okay, it reads pretty clearly to me - but this is where the personal
preference business kicks in.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Aidy - 14 Mar 2008 15:21 GMT
> Show me *any* production application where that makes any significant
> difference, and I'll buy you several pints. It's really not worth
> bothering about.

I've admitted it doesn't make a difference, it just comes down to personal
choice.  However it seems you're not too keen when other's personal choice
differs from your personal choice!
Jon Skeet [C# MVP] - 14 Mar 2008 15:35 GMT
> > Show me *any* production application where that makes any significant
> > difference, and I'll buy you several pints. It's really not worth
[quoted text clipped - 3 lines]
> choice.  However it seems you're not too keen when other's personal choice
> differs from your personal choice!

Not at all - I'm perfectly happy if people choose to use a different
approach based on their personal preference due to differing ideas of
readability.

If people choose to use one approach based on *performance* however,
they're using a completely bogus reason - and I'll call them on that.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Norbert Unterberg - 14 Mar 2008 15:15 GMT
jsh02_nova schrieb:
> Which code is really better in term of performance, A or B?
> A) list_of_strings[0] == ""
> B) list_of_strings[0].Length.Equals(0)

Depending on what exactly you want to check, you could also use
    String.IsNullOrEmpty(list_of_strings[0])
or
    list_of_strings[0] == string.Empty

This avoids constructing a temporary string object that holds the emtpy
string for comparison.

Norbert
Jon Skeet [C# MVP] - 14 Mar 2008 15:34 GMT
> jsh02_nova schrieb:
> > Which code is really better in term of performance, A or B?
[quoted text clipped - 8 lines]
> This avoids constructing a temporary string object that holds the emtpy
> string for comparison.

There would be no temporary string object. There'd be a single interned
empty string for every occurrence of the string literal "" in your
AppDomain.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Alvin Bruney [ASP.NET MVP] - 14 Mar 2008 17:09 GMT
Is that correct? string interning isn't on by default is it?

Signature

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99

>> jsh02_nova schrieb:
>> > Which code is really better in term of performance, A or B?
[quoted text clipped - 12 lines]
> empty string for every occurrence of the string literal "" in your
> AppDomain.
Jon Skeet [C# MVP] - 14 Mar 2008 17:31 GMT
> Is that correct? string interning isn't on by default is it?

For C#, it's guaranteed by the language specification.

Section 2.4.4.5 (unified C# 3.0 spec):

<quote>
Each string literal does not necessarily result in a new string
instance. When two or more string literals that are equivalent
according to the string equality operator (§7.9.7) appear in the same
program, these string literals refer to the same string instance.
</quote>

That also includes "x"+"y" being the same reference as "xy" btw.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

schneider - 14 Mar 2008 18:30 GMT
I think Fx Cop says to use .Length

I like then Length myself.

Schneider

<"Alvin Bruney [ASP.NET MVP]" <www.lulu.com/owc>> wrote:
> Is that correct? string interning isn't on by default is it?

For C#, it's guaranteed by the language specification.

Section 2.4.4.5 (unified C# 3.0 spec):

<quote>
Each string literal does not necessarily result in a new string
instance. When two or more string literals that are equivalent
according to the string equality operator (§7.9.7) appear in the same
program, these string literals refer to the same string instance.
</quote>

That also includes "x"+"y" being the same reference as "xy" btw.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

schneider - 14 Mar 2008 19:54 GMT
If I remember correctly all unique strings are cache, at what level I'm not
sure...

Schneider

>I think Fx Cop says to use .Length
>
[quoted text clipped - 17 lines]
>
> That also includes "x"+"y" being the same reference as "xy" btw.
Jon Skeet [C# MVP] - 14 Mar 2008 20:25 GMT
> If I remember correctly all unique strings are cache, at what level I'm not
> sure...

No, only those which are interned - which includes all string literals.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

schneider - 14 Mar 2008 21:52 GMT
Correct, I was under the impression it was automatic. Seems there is some GC
issues related to them also.
http://msdn2.microsoft.com/en-us/library/system.string.intern.aspx

>> If I remember correctly all unique strings are cache, at what level I'm
>> not
>> sure...
>
> No, only those which are interned - which includes all string literals.
Jon Skeet [C# MVP] - 14 Mar 2008 22:03 GMT
> Correct, I was under the impression it was automatic. Seems there is some GC
> issues related to them also.
> http://msdn2.microsoft.com/en-us/library/system.string.intern.aspx

Automatically interning *every* string to ever be created would be a
memory nightmare. Basically it would make it impossible for an
application to stay up for any significant length of time, if it was
doing any string processing.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Alvin Bruney [ASP.NET MVP] - 16 Mar 2008 02:06 GMT
uh huh. i had it backward. that jogged my memory.

Signature

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99

>> Is that correct? string interning isn't on by default is it?
>
[quoted text clipped - 10 lines]
>
> That also includes "x"+"y" being the same reference as "xy" btw.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.