473,387 Members | 1,624 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,387 software developers and data experts.

Optimize switch for speed

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 target1;
string target2;

switch (key) {
case "Text of some kind":
target1 = value;
break;
case "Text of some other kind":
target2 = value;
break;
// etc.

The list of possible cases is long, and I would like to avoid a linear
search through all possible cases. I can't just use a hash table
cause each case statement refers to a different target loc for storing
the 'value' part.

Any ideas?

Nov 15 '05 #1
8 2013
_eddie <_fakeemail@_nospam_.com> wrote:
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 target1;
string target2;

switch (key) {
case "Text of some kind":
target1 = value;
break;
case "Text of some other kind":
target2 = value;
break;
// etc.

The list of possible cases is long, and I would like to avoid a linear
search through all possible cases. I can't just use a hash table
cause each case statement refers to a different target loc for storing
the 'value' part.


It basically *is* going to be a linear search, but it won't need to
actually check every character in the text, as switches on strings make
cunning use of interning.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
On Fri, 23 Jan 2004 19:51:08 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
_eddie <_fakeemail@_nospam_.com> wrote:
// key = "Text of some kind" // value = "Value Text"

string target1;
string target2;

switch (key) {
case "Text of some kind":
target1 = value;
break;
case "Text of some other kind":
target2 = value;
break;
// etc.
It basically *is* going to be a linear search, but it won't need to
actually check every character in the text, as switches on strings make
cunning use of interning.


So it is already optimized about as much as it can be, or does the
order of the case statements have some bearing?

I was also considering reducing the number of case statements and just
not parsing some less valuable data, but if the cases are already
effectively hashed, then that wouldn't help much.

e

PS: Where did you find out about 'interning'?

Nov 15 '05 #3
_eddie <_fakeemail@_nospam_.com> wrote:
It basically *is* going to be a linear search, but it won't need to
actually check every character in the text, as switches on strings make
cunning use of interning.
So it is already optimized about as much as it can be, or does the
order of the case statements have some bearing?


I very much doubt that it matters much, but if you *really* think it's
having a bearing on performance, you could always try different things
and measure the results.
I was also considering reducing the number of case statements and just
not parsing some less valuable data, but if the cases are already
effectively hashed, then that wouldn't help much.
Indeed.

How often are you actually executing this switch? Are you doing
*anything* significant within the cases themselves? I would be very
surprised if you found that the actual bottleneck in your application
is the switch.
PS: Where did you find out about 'interning'?


The C# language specification, the CLR specification, general
digging...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
If you have enough entries, the compiler will switch over to a hashtable
rather than a linear search. IIRC, that switch occurs when the number of
cases is in the teens (16 comes to mind, but don't quote me on that).

You can tell by writing a big string switch and looking at the generated
code.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"_eddie" <_fakeemail@_nospam_.com> wrote in message
news:th********************************@4ax.com...
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 target1;
string target2;

switch (key) {
case "Text of some kind":
target1 = value;
break;
case "Text of some other kind":
target2 = value;
break;
// etc.

The list of possible cases is long, and I would like to avoid a linear
search through all possible cases. I can't just use a hash table
cause each case statement refers to a different target loc for storing
the 'value' part.

Any ideas?

Nov 15 '05 #5
On Fri, 23 Jan 2004 22:43:40 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
_eddie <_fakeemail@_nospam_.com> wrote:
>It basically *is* going to be a linear search, but it won't need to
>actually check every character in the text, as switches on strings make
>cunning use of interning.
So it is already optimized about as much as it can be, or does the
order of the case statements have some bearing?


I very much doubt that it matters much, but if you *really* think it's
having a bearing on performance, you could always try different things
and measure the results.


Unfortunately, I can't think of anything else to try. <g> The target
locations change, so I can't just save refs to the target locs as
values in a hash table. The incoming data is text, so it would take
time to generate hash codes anyway.
How often are you actually executing this switch? Are you doing
*anything* significant within the cases themselves? I would be very
surprised if you found that the actual bottleneck in your application
is the switch.


That's pretty much all that the program is doing. It spins in a loop,
parsing incoming text with the switch, and storing the corresponding
values in various locations. The only thing I can eliminate is a few
of the case statements.

It seems slow--about 1-1/2 minutes to process a 125 meg text file, but
maybe that's all she'll do!
PS: Where did you find out about 'interning'?


The C# language specification, the CLR specification, general
digging...


Good insight into the switch function. Thanks.
Nov 15 '05 #6
_eddie <_fakeemail@_nospam_.com> wrote:
How often are you actually executing this switch? Are you doing
*anything* significant within the cases themselves? I would be very
surprised if you found that the actual bottleneck in your application
is the switch.


That's pretty much all that the program is doing. It spins in a loop,
parsing incoming text with the switch, and storing the corresponding
values in various locations. The only thing I can eliminate is a few
of the case statements.

It seems slow--about 1-1/2 minutes to process a 125 meg text file, but
maybe that's all she'll do!


How complicated is your code? Could you post it? You may be doing
something else to cause the bottleneck. How fast it is at *just*
reading the file?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
On Fri, 23 Jan 2004 15:39:55 -0800, "Eric Gunnerson [MS]"
<er****@online.microsoft.com> wrote:
If you have enough entries, the compiler will switch over to a hashtable
rather than a linear search. IIRC, that switch occurs when the number of
cases is in the teens (16 comes to mind, but don't quote me on that).

You can tell by writing a big string switch and looking at the generated
code.

I did a test, and at 10 strings, it switches over to a hashtable.

Austin
Nov 15 '05 #8
On Sat, 24 Jan 2004 00:12:08 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
_eddie <_fakeemail@_nospam_.com> wrote:
>How often are you actually executing this switch? Are you doing
>*anything* significant within the cases themselves? I would be very
>surprised if you found that the actual bottleneck in your application
>is the switch.


That's pretty much all that the program is doing. It spins in a loop,
parsing incoming text with the switch, and storing the corresponding
values in various locations. The only thing I can eliminate is a few
of the case statements.

It seems slow--about 1-1/2 minutes to process a 125 meg text file, but
maybe that's all she'll do!


How complicated is your code? Could you post it? You may be doing
something else to cause the bottleneck. How fast it is at *just*
reading the file?


This part of the code is relatively simple, but there are a lot of
case statements. Unfortunately, I can't post it, but I have tried
some tests, with inconsistent results. In general, NOT running the
switch cuts the run time in half.

From other replies, it looks like the switch is already hashed, so
I'll live with the current speed. I thought maybe there was a better
method for this type of thing, but apparently this is it.

I was also trying to avoid stalling the UI, so I'll have to create a
thread for that task (and I suppose have to marshal the data back to
the UI thread).

Thanks for your help, Jon.

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
5
by: Markus Dehmann | last post by:
Profiling has shown that during runtime of my program, 100 million objects of class X are constructed and destructed. What is the best way to optimize in such a case, both for memory and speed...
11
by: Michael B. | last post by:
I'm still learning C so I've written a simple app which lets you make a contact list (stored as a linked list of structs), write it to a file, and read it back. It works fine, but I notice in my...
3
by: Reddy | last post by:
The sql query for my datagrid returns 100, 000 records. But the datagrid should display 20 records per page. I am using datagrid paging, but it is taking too much time for the page to load. Is...
3
by: zhangyue.zl | last post by:
For a source file ,which optimize option can make gcc generate a smaller object file,O2 or O3? Thanks!
13
by: Frank Swarbrick | last post by:
IBM has a product for the VSE operating system called the VSAM Redirector. It allows you to use VSAM to access RDBMS tables/views as if they were actual VSAM files. We're doing a comparison right...
15
by: kenneth | last post by:
I was trying to use multiple thread to optimize my following code, but met some problems, anyone can help me? k are initialized. int computePot() { int i, j; for( i=0; i<500; i++ ) { for(...
2
by: pavanip | last post by:
Hi, I have an application like Optimize System Performance by using Memory speed, cpu speed and Disk speed. How to optimize memory speed,disk optimization and CPU optimization. Please provide me...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.