Multimobile Development: Building Applications for any Smartphone
String.Empty and performance
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.performance.


Bob Grommes
I have been wondering why one would use String.Empty rather than the constant
"" since it would seem that "" is not platform-dependent. Then it occurred to
me that at runtime probably "" is converted to a string object and maybe this
overhead is avoided with String.Empty. On the other hand maybe the C# compiler
performs this simple optimization anyway.

Basically I'm just trying to determine if String.Empty is any kind of
performance win or if it's just an option some people might like for making
code more self-evident (personally I think it's probably overkill in that
regard, but to each his/her own).

--Bob Grommes

Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
Nick Wienholt
Perf. tests for an empty static string. Tests run for 1000000 loops.

String.Empty Test:
Normalised: 2.679032 Median: 00:00:00.0135050 Mean: 00:00:00.0140000 Min:
00:00:00.0134460 Max: 00:00:00.0139620 StdDev: 00:00:00
Results: 00:00:00.0139620 00:00:00.0135050 00:00:00.0137790 00:00:00.0134650
00:00:00.0134460

"" Test:
Normalised: 2.672882 Median: 00:00:00.0134740 Mean: 00:00:00.0130000 Min:
00:00:00.0134170 Max: 00:00:00.0135000 StdDev: 00:00:00
Results: 00:00:00.0135000 00:00:00.0134950 00:00:00.0134170 00:00:00.0134170
00:00:00.0134740

String.Length == 0 Test:
Normalised: 1 Median: 00:00:00.0050410 Mean: 00:00:00.0050000 Min:
00:00:00.0050240 Max: 00:00:00.0054420 StdDev: 00:00:00
Results: 00:00:00.0054420 00:00:00.0050240 00:00:00.0053690 00:00:00.0050410
00:00:00.0050240

String.Length == 0 is over twice as quick.

Nick Wienholt
Sydney Deep .NET User Group www.sdnug.org

"Bob Grommes" <Click here to reveal e-mail address> wrote in message
news:uiZyBgTGCHA.2392@tkmsftngp04...
> I have been wondering why one would use String.Empty rather than the
constant
> "" since it would seem that "" is not platform-dependent. Then it
occurred to
> me that at runtime probably "" is converted to a string object and maybe
this
> overhead is avoided with String.Empty. On the other hand maybe the C#
compiler
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
    
Bob Grommes
Wow, thanks Nick.

Looks like String.Length is the optimization the compiler *ought* to emit in
place of a test for equality to "".

So now the question remains, what the heck is String.Empty there for? I guess
it must just be someone's pet syntax.

--Bob

"Nick Wienholt" <Click here to reveal e-mail address> wrote in message
news:OMJUbgYGCHA.2680@tkmsftngp12...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
James F. Bellinger
It's probably an interned version. Save a couple bytes of memory, maybe.

"Bob Grommes" <Click here to reveal e-mail address> wrote in message
news:uqsd3KaGCHA.2280@tkmsftngp12...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
First chapters of Multimobile Development book now available on Apress Alpha program
iPad
 
    
Bob Grommes
Actually, I eventually found a reference somewhere that String.Empty is a
"language-independent way of expressing an empty string". I'm not sure what
language would not have its own semantic representation of an empty string ...
but it's as good a reason as any for the construct, I suppose.

--Bob

"James F. Bellinger" <Click here to reveal e-mail address> wrote in message
news:4X3R8.93111$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
New book project – Multimobile Development: Building Applications for any Smartphone
Dive into HTML5
 
    
Ryan Nichols
It's useful because if a string variable is nothing, you can compare it against string.Empty without throwing an exception.

Consider this:

Dim s as String = Nothing

If s = String.Empty Then
'Do Something
End If

Without String.Empty, you would have to check for both 'Nothing' and "" to have it pass.

--------------------------------
From: Ryan Nichols
Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Daniel Moth
How about
Boolean.TrueString

Cheers
Daniel

"Bob Grommes" <Click here to reveal e-mail address> wrote in message
news:uiZyBgTGCHA.2392@tkmsftngp04...
> I have been wondering why one would use String.Empty rather than the
constant
> "" since it would seem that "" is not platform-dependent. Then it
occurred to
> me that at runtime probably "" is converted to a string object and maybe
this
> overhead is avoided with String.Empty. On the other hand maybe the C#
compiler
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Steve Jobs’ thoughtful/thought provoking Thoughts on Flash…
Handy list of countries in CSV format
 
    
Simon Stewart
Wouldn't the most performant way of checking for a value in a string be to
check for a zero length?
At least that was the prescribed way for VB6 apps.

HTH

Simon

"Daniel Moth" <Click here to reveal e-mail address> wrote in message
news:#XpjXBNLCHA.2256@tkmsftngp13...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
Daniel Moth
Sorry I mean is it faster to use Boolean.TrueString insetad of "True" in
code?

BTW, If you use the VB6 way (Len(someString)>0) the .NET way
(someString.Length()>0) be sure that someString is Not Nothing...

"Simon Stewart" <Click here to reveal e-mail address> wrote in message
news:uh#cq#NLCHA.2424@tkmsftngp09...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
James F. Bellinger
Well, it wouldn't involve another instance of a string being created.
Further, it'd be portable against different versions of the Framework
where it might change to "Yes" or something... :-)

"Daniel Moth" <Click here to reveal e-mail address> wrote in message
news:eKZ#pNOLCHA.1944@tkmsftngp13...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
 
System.Boolean
System.String




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734