471,317 Members | 2,156 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Speed of switch/case vs hash table for parsing XML

_DS
I'm currently using a switch with about 50 case statements in a
stretch of code that's parsing XML attributes. Each case is a string.
I'm told that switch statements will actually use hash tables when
number of cases is around 10 or more, so I haven't changed the code.
Just wondering if anyone knows about how that's structured internally.
It does seem a bit slow.

Feb 11 '06 #1
5 3796
_DS <_D*@nomail.com> wrote:
I'm currently using a switch with about 50 case statements in a
stretch of code that's parsing XML attributes. Each case is a string.
I'm told that switch statements will actually use hash tables when
number of cases is around 10 or more, so I haven't changed the code.
Just wondering if anyone knows about how that's structured internally.
It does seem a bit slow.


What exactly do you mean by the last sentence? Do you mean you've
implemented some code and it's working slowly, or that it sounds like a
slow way of doing things?

I suspect if you're really seeing something working slowly, it won't be
due to the switch statement. I very much doubt that that will be the
bottleneck.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 11 '06 #2
I'm almost certain that the C# compiler will not implement the select
construct with a Hashtable. You can easily check by running ildasm on
the assembly. Either way 50 case statements is an awful lot. If
nothing else I recommend refactoring the code to gain readability.

Brian

_DS wrote:
I'm currently using a switch with about 50 case statements in a
stretch of code that's parsing XML attributes. Each case is a string.
I'm told that switch statements will actually use hash tables when
number of cases is around 10 or more, so I haven't changed the code.
Just wondering if anyone knows about how that's structured internally.
It does seem a bit slow.


Feb 11 '06 #3
Brian Gideon <br*********@yahoo.com> wrote:
I'm almost certain that the C# compiler will not implement the select
construct with a Hashtable. You can easily check by running ildasm on
the assembly. Either way 50 case statements is an awful lot. If
nothing else I recommend refactoring the code to gain readability.


It's easy to verify that at least in the case of strings, the C#
compiler does indeed use a hash table. Compile the following and then
disassemble it:

class Test
{
static void Main (string[] args)
{
int i=0;
switch (args[0])
{
case "1":
case "2":
case "3":
case "4":
case "5":
i=1;
break;
case "6":
case "7":
case "8":
case "9":
case "10":
i=2;
break;
}
i++;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 11 '06 #4
Jon,

That's actually pretty cool. I suppose if it does it for strings it
would do it for others as well. I never would have thought...

Brian

Jon wrote:
Brian Gideon <br*********@yahoo.com> wrote:
I'm almost certain that the C# compiler will not implement the select
construct with a Hashtable. You can easily check by running ildasm on
the assembly. Either way 50 case statements is an awful lot. If
nothing else I recommend refactoring the code to gain readability.


It's easy to verify that at least in the case of strings, the C#
compiler does indeed use a hash table. Compile the following and then
disassemble it:

class Test
{
static void Main (string[] args)
{
int i=0;
switch (args[0])
{
case "1":
case "2":
case "3":
case "4":
case "5":
i=1;
break;
case "6":
case "7":
case "8":
case "9":
case "10":
i=2;
break;
}
i++;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Feb 11 '06 #5
Brian Gideon <br*********@yahoo.com> wrote:
That's actually pretty cool. I suppose if it does it for strings it
would do it for others as well. I never would have thought...


It doesn't actually need to do it for other types. The thing is that
the IL switch instruction only supports integral types, so the C#
compiler needs to "fake it" for string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

35 posts views Thread by Thomas Matthews | last post: by
8 posts views Thread by _eddie | last post: by
11 posts views Thread by Sezai YILMAZ | last post: by
11 posts views Thread by ME | last post: by
7 posts views Thread by Michael | last post: by
1 post views Thread by MariuszC | last post: by
21 posts views Thread by aaron80v | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.