dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [DotGNU]fxcop


From: Miroslaw Dobrzanski-Neumann
Subject: Re: [DotGNU]fxcop
Date: Fri, 30 Apr 2004 14:59:16 +0200
User-agent: Mutt/1.4i

On Sat, May 01, 2004 at 12:32:32AM +1200, Thong (Tum) Nguyen wrote:
> It really shouldn't be optimized out.
> 
> [ if (val == "") ] doesn't throw a NullReferenceException if val is null
> whereas [ if (val.Length == 0) ] will.
> 
> IMO, the appropriate optimisation would be to replace "" with String.Empty.

here are my run times:
if (a == "")            4.99s
if (a.Length == 0)      2.82s
if (a == string.Empty) 11.94s

thus a == string.Empty is not an option
how about this replacement:

if (a != null && a.Length == 0)

> 
> Regards,
> 
> ^Tum
> 
> > -----Original Message-----
> > From: Miroslaw Dobrzanski-Neumann [mailto:address@hidden
> > Sent: Saturday, 1 May 2004 12:12 a.m.
> > To: PNET-D
> > Subject: [DotGNU]fxcop
> > 
> > Hello,
> > 
> > do you know FxCop?
> > 
> > FxCop ist a kind of lint that analyzes assemblies for coding and
> > performance
> > problems.
> > 
> > One of the fixed ideas (validation rules) of fxcop reads (this is not a
> > citation):
> > Do not test for empty string comparing it to empty string literal:
> > if (val == "")
> >     ...
> > use its length instead:
> > if (val.Length == 0)
> >     ...
> > because comparing strings (empty literal will be converted to an string
> > instance before comparison) is much more expensive than comaring the
> > string
> > length.
> > 
> > I have tested it with pnet and indeed pnet runs *val.Length == 0* two
> > times
> > faster than *val == ""*.
> > I have not tested other runtimes because it is not always allowed :-(.
> > But if FxCop says so it must be true for them too.
> > 
> > My question is if pnet could do it better?
> > Especially this trivial kind of optimization could be done directly in
> > parser.
> > It would have no impact on anything.
> > 
> > Regards
> > --
> > Miroslaw Dobrzanski-Neumann
> > E-mail: address@hidden
> > 
> > This message is utf-8 encoded
> > _______________________________________________
> > Developers mailing list
> > address@hidden
> > http://dotgnu.org/mailman/listinfo/developers
> 

-- 
Mirosław Dobrzański-Neumann

MOSAIC SOFTWARE AG
IT Berater für Vorstandsprojekte
Tel +49-2225-882-291
Fax +49-2225-882-201
E-mail: address@hidden

This message is utf-8 encoded


reply via email to

[Prev in Thread] Current Thread [Next in Thread]