Groups | Blog | Home
all groups > dotnet performance > march 2008 >

dotnet performance : Better coding


jsh02_nova
3/13/2008 8:04:01 PM
Which code is really better in term of performance, A or B?
A) list_of_strings[0] == ""
Mufaka
3/13/2008 8:18:38 PM
[quoted text, click to view]

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.

Aidy
3/14/2008 9:59:51 AM
[quoted text, click to view]

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]
3/14/2008 10:21:04 AM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Aidy
3/14/2008 11:29:52 AM
[quoted text, click to view]

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]
3/14/2008 12:04:14 PM
[quoted text, click to view]

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.

[quoted text, click to view]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Alvin Bruney [ASP.NET MVP]
3/14/2008 12:09:07 PM
Is that correct? string interning isn't on by default is it?

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


[quoted text, click to view]
schneider
3/14/2008 12:30:30 PM
I think Fx Cop says to use .Length

I like then Length myself.

Schneider


[quoted text, click to view]

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.

--
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
3/14/2008 1:54:25 PM
If I remember correctly all unique strings are cache, at what level I'm not
sure...

Schneider

[quoted text, click to view]

Aidy
3/14/2008 2:21:27 PM
[quoted text, click to view]

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]
3/14/2008 2:34:12 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Jon Skeet [C# MVP]
3/14/2008 2:35:38 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Norbert Unterberg
3/14/2008 3:15:55 PM
jsh02_nova schrieb:
[quoted text, click to view]

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.

schneider
3/14/2008 3:52:47 PM
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



[quoted text, click to view]

Jon Skeet [C# MVP]
3/14/2008 4:31:42 PM
[quoted text, click to view]

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=20
instance. When two or more string literals that are equivalent=20
according to the string equality operator (=A77.9.7) appear in the same=20
program, these string literals refer to the same string instance.=20
</quote>

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

--=20
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Jon Skeet [C# MVP]
3/14/2008 7:25:55 PM
[quoted text, click to view]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Jon Skeet [C# MVP]
3/14/2008 9:03:30 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Alvin Bruney [ASP.NET MVP]
3/15/2008 9:58:50 PM
uh huh. i had it backward. that jogged my memory.

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


[quoted text, click to view]
AddThis Social Bookmark Button