473,385 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3897
_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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
8
by: _eddie | last post by:
Is there a good way to code a switch/case-type construct for maximal speed? The goal is to parse text key/value pairs. IOW: // key = "Text of some kind" // value = "Value Text" string...
11
by: Sezai YILMAZ | last post by:
Hello I need high throughput while inserting into PostgreSQL. Because of that I did some PostgreSQL insert performance tests. ------------------------------------------------------------ --...
6
by: jao | last post by:
My company has a product in beta which uses Postgres 7.4.3. We expect to have a code freeze for our 1.0 product in March 2005. I'd really like to use Postgres 8.x in our 1.0 product. We're...
11
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test...
7
by: Michael | last post by:
I'm writing an application that decodes a file containing binary records. Each record is a particular event type. Each record is translated into ASCII and then written to a file. Each file contains...
1
by: MariuszC | last post by:
Hello, I have used some library where is defined enum like: typedef enum Enum1 { ENUM1_1, ENUM1_2, ENUM1_3, ENUM1_4, ENUM1_5 }; In my code I have defined: typedef enum Enum2 { ENUM2_1,...
21
by: aaron80v | last post by:
Hi, A simple question: In ANSI-C, how to specify a string as the expression for switch statement. eg switch (mystr) { case "ABC" : bleh... break;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.