Speed of switch/case vs hash table for parsing XML

  • Thread starter Thread starter _DS
  • Start date Start date
D

_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.
 
_DS said:
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.
 
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
 
Brian Gideon said:
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,

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
Brian Gideon said:
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++;
}
}
 
Brian Gideon said:
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.
 
Back
Top