Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
String.Empty and performance
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.performance.


Bob Grommes
GOOD ANSWER
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... (2 votes from other users already)
 
 
    
Nick Wienholt
GOOD ANSWER
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... (2 votes from other users already)
 
 
    
Bob Grommes
GOOD ANSWER
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... (1 vote from another user already)
 
 
    
James F. Bellinger
GOOD ANSWER
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... (1 vote from another user already)
 
 
    
Bob Grommes
GOOD ANSWER
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... (1 vote from another user already)
 
 
    
Ryan Nichols
GOOD ANSWER
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... (1 vote from another user already)
 
 
    
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...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
Simon Stewart
GOOD ANSWER
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... (1 vote from another user already)
 
 
    
Daniel Moth
GOOD ANSWER
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... (1 vote from another user already)
 
 
    
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...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
 
System.Boolean
System.String




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734