473,669 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2026
_eddie <_fakeemail@_no spam_.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.co m>
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.co m> wrote:
_eddie <_fakeemail@_no spam_.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@_no spam_.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.co m>
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@_no spam_.com> wrote in message
news:th******** *************** *********@4ax.c om...
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.co m> wrote:
_eddie <_fakeemail@_no spam_.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@_no spam_.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.co m>
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.co m> wrote:
_eddie <_fakeemail@_no spam_.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
2392
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'; $sentence = "$insert or not $insert. That is the question."; or
5
1932
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 (speed maybe more important)? One idea is to make the class very small, to contain only the most important member data, and put everything else, including member functions, into another class:
11
2127
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 loading procedure there's a point or two where I'll malloc a struct which I then don't need, and dispose of it; I was wondering if there was some way I could optimize this kludge out of existence: void loadContacts () { int fd, bytesread; if (...
3
2820
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 there any way I can optimize the speed. Any sample code would be great. Thanks, Reddy
3
2401
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
3112
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 now between Oracle, which we've been running in production for many years, and DB2, which we are just starting with. One of the tests I am trying is to see how efficient the VSAM Redirector works with DB2 versus Oracle. Below are two types of...
15
2517
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( j=0; j<i-1; j++ ) {
2
2081
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 some sample source code to optimize system performance. Thanks pavani
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8895
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.