[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

switch language extension

Mark

8/20/2008 1:38:00 PM

Hi...

I read http://www.blackwasp.co.uk/SpeedTestIfElseS... 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

4 Answers

jetan

8/21/2008 3:36:00 AM

0

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=...

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.


Patrice

8/21/2008 8:08:00 AM

0

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/SpeedTestIfElseS... 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
>

Mark

8/25/2008 4:20:00 PM

0

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

jetan

8/28/2008 2:21:00 AM

0

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=...

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.