.NETGURU
String.Empty and performance
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.performance.

Post a new message to this list...

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...
 
    
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...
 
    
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...
 
    
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...
 
    
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...
 
    
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...
 
    
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...
 
    
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...
 
    
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...
 
    
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...
 
 
System.Boolean
System.String




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification