Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Wed, 20 Aug 2008 06:38:25 -0700,    group: microsoft.public.dotnet.framework        back       


switch language extension   
Hi...

I read http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx on switch 
optimizations, and I played with some examples in ildasm to see what kind of 
optimizations are used.

Then I ran some tests switching on strings with case normalization (i.e.
switch (variable.ToLower())
{
    case "foo":...
    case "bar":...
}).  Not too surprisingly, the case normalization was a big hit (between 7x 
and 10x).

I was thinking if switch had an optional 2nd argument for a comparitor, you 
could get the best of both worlds - e.g.

switch (variable, StringComparer.OrdinalIgnoreCase)
{
...
}

Would that be considered as a language extension?

Thanks
Mark
date: Wed, 20 Aug 2008 06:38:25 -0700   author:   Mark am

RE: switch language extension   
Hi Mark,

If I have understood you completely, you are suggesting adding the second 
parameter to the C# "switch" keyword. 

First, this second parameter only applies to the string object type, so the 
"switch" keyword must support variable parameters list.  I am not a 
compilation expert, but I did not see the cases that C# language keyword 
supports variable parameter list. So I suspect if this can be supported by 
"switch" keyword.

Second, if this feature can be supported, it should be a C# language 
extension which should not be part of the Command Language 
Specification(CLS). It will become the Microsoft specific extension. 

Anyway, the best way to feedback you suggestion is using the feedback 
center below:
https://connect.microsoft.com/VisualStudio?wa=wsignin1.0

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and 
suggestions about how we can improve the support we provide to you. Please 
feel free to let my manager know what you think of the level of service 
provided. You can send feedback directly to my manager at: 
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
date: Thu, 21 Aug 2008 03:36:05 GMT   author:   (Jeffrey Tan[MSFT])

Re: switch language extension   
Keep in mind thought that when comparing times you have also to take into 
account the absolute time it takes ie running 10 x faster something that 
last 0,002 s won't produce anything visible while going from 20 s to 2 s is 
noticeable.

So :
- IMO this is a micro-optimization that really doesn't worth in most cases
- if you had a real life huge loop a best practice would be to normalize the 
value outside of the loop not in each iteration (IMO you ran the ToLower in 
each ean every loop)
- if case is really not important it could be also because an enum would be 
more suitable

IMO all this remains quite theoricall for now. I would much prefer a real 
life scenario...

Also I'm not really sure to understand how it could help. If you tell C# to 
ignore case then behind the scene the code still has additional work to do 
something similar to what you have done explicitely...

IMO in a real life situation, a solution would be to normalize the tested 
value once for all before entering the loop...


--
Patrice

"Mark" <mmodrall@nospam.nospam> a écrit dans le message de groupe de 
discussion : 1B467F43-E1C8-40DF-8910-82B6DE922CDD@microsoft.com...
> Hi...
>
> I read http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx on switch
> optimizations, and I played with some examples in ildasm to see what kind 
> of
> optimizations are used.
>
> Then I ran some tests switching on strings with case normalization (i.e.
> switch (variable.ToLower())
> {
>    case "foo":...
>    case "bar":...
> }).  Not too surprisingly, the case normalization was a big hit (between 
> 7x
> and 10x).
>
> I was thinking if switch had an optional 2nd argument for a comparitor, 
> you
> could get the best of both worlds - e.g.
>
> switch (variable, StringComparer.OrdinalIgnoreCase)
> {
> ...
> }
>
> Would that be considered as a language extension?
>
> Thanks
> Mark
>
date: Thu, 21 Aug 2008 10:08:05 +0200   author:   Patrice http://www.chez.com/scribe/

Re: switch language extension   
Well, personally I think the appeal of case-insensativity is vastly 
overblown, but it often comes up on serialization boundaries, messaging 
systems, and configuration files.  I've inherited a code base obsessed with 
case-insensativity so I see the original pattern everywhere.

> - IMO this is a micro-optimization that really doesn't worth in most cases
> - if you had a real life huge loop a best practice would be to normalize the 
> value outside of the loop not in each iteration (IMO you ran the ToLower in 
> each ean every loop)

Yes, but time testing often means you replicate what the code does and wrap 
a big loop around it.  You can't optimize the loop invariants outside the 
loop because that defeats the purpose of testing one pattern against the 
other.

> IMO all this remains quite theoricall for now. I would much prefer a real 
> life scenario...
> 
> Also I'm not really sure to understand how it could help. If you tell C# to 
> ignore case then behind the scene the code still has additional work to do 
> something similar to what you have done explicitely...

The question is what "additional work" gets done.  The String.ToLower() call 
*guarantees* at least 1 full pass through the input and the possible creation 
of a copy object which puts additional load on the garbage collection.  
Depending on the app circumstances the extra object in the pool is 10x the 
work.

Another way to replace the
switch (input.ToLower())
{...}

pattern is to use
if (input.Equals("1stcase", StringComparison.OrdinalIgnoreCase))
...
else if (input.Equal("2ndcase", StringComparison.OrdinalIgnoreCase))
...
etc.

Aside from the verbosity, with the 2nd pattern you don't make another copy 
of the input and you keep all the advantages in comparison (length test, 
object identity test).  The only "additional work" in the 2nd pattern is one 
character case test when you haven't matched.

But the 2nd pattern loses the flexibility of switch for optimization because 
the ifs have to be executed in order.

Looking at some sample switch optimizations in ildasm, it seems that for 
string operands with > 10 cases, the compiler builds a Dictionary<string, 
int> jump vector in memory, so you get the benefit of hash table lookup 
speeds.

If we had a comparitor option operand on switch, it could build a
new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
instead.

That way you get the switch optimization and case insensitivity without the 
hit ToLower() brings.

Yes, you could consider this a micro optimization, but MS puts out other 
whitepapers suggesting 
if (str.Length == 0)
instead of
if (str == "")
and advising how many concatenations you should do before using StringBuilder.

Mark
date: Mon, 25 Aug 2008 09:20:00 -0700   author:   Mark am

Re: switch language extension   
Hi Mark,

Yes, I think your analysis makes sense.

I agree that this micro optimization can be useful if the method that 
contains the "switch" sentense is called frequently. We definitely should 
use performance profiling to confirm that this method is the bottleneck. 
Once we confirm this as the bottleneck, any performance optimization in the 
method is a huge boost.

I still recommend you to submit your suggestion using the feedback center 
below, the VC# team will give an official informative follow up with you:
https://connect.microsoft.com/VisualStudio?wa=wsignin1.0

If you have any feedback from the VC# team, please feel free to share here. 
Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and 
suggestions about how we can improve the support we provide to you. Please 
feel free to let my manager know what you think of the level of service 
provided. You can send feedback directly to my manager at: 
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
date: Thu, 28 Aug 2008 02:21:25 GMT   author:   (Jeffrey Tan[MSFT])

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us