473,796 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch statement optimizations

I have a single switch statement with about 30 case statements.

I could break the single switch into 3 or 4 switchs, which logically
would be more efficient.

switch()
{
switch() // only 10 cases

switch() // only 10 cases
}
But with compiler optimizations would it make any difference? Would
one way be better than the other?

I'm using MSVC 6
Jul 19 '05 #1
5 4282
On 18 Sep 2003 09:12:42 -0700, ro************* @gd-decisionsystems .com (Rob Adelberg) wrote:
I have a single switch statement with about 30 case statements.
Perhaps you're stuck with someone else's code.
I could break the single switch into 3 or 4 switchs, which logically
would be more efficient.
Nope, no logic in that.
switch()
{
switch() // only 10 cases

switch() // only 10 cases
}
But with compiler optimizations would it make any difference? Would
one way be better than the other?
The C++ standard has nothing to say about that.
I'm using MSVC 6


Then ask in a Microsoft group (use news.microsoft. com if necessary),
it's off-topic here (see the FAQ).

Jul 19 '05 #2
Rob Adelberg wrote:
I have a single switch statement with about 30 case statements.

I could break the single switch into 3 or 4 switchs, which logically
would be more efficient.
Huh?
switch()
{
switch() // only 10 cases

switch() // only 10 cases
}
But with compiler optimizations would it make any difference? Would
one way be better than the other?

I'm using MSVC


Switch is often a computed goto, especially if the cases are
reasonably contiguous. (Ooh, deja vu. I must be paraphrasing
someone, but I can't remember whom.) So with a single switch
you get a single computation and a single goto. In any case,
it's the compiler's job to choose the most efficient
implementation.

Regards,
Buster.

Jul 19 '05 #3
Buster Copley wrote:
Rob Adelberg wrote:
I have a single switch statement with about 30 case statements.

I could break the single switch into 3 or 4 switchs, which logically
would be more efficient.

Huh?
switch()
{
switch() // only 10 cases

switch() // only 10 cases
}
But with compiler optimizations would it make any difference? Would
one way be better than the other?

I'm using MSVC

Switch is often a computed goto, especially if the cases are
reasonably contiguous.


Switch _is_ a computed goto, obviously. Sorry. The person I was
paraphrasing was saying that it's often implemented using a jump
table.

Jul 19 '05 #4
Rob Adelberg wrote:
I have a single switch statement with about 30 case statements.

I could break the single switch into 3 or 4 switchs, which logically
would be more efficient.

switch()
{
switch() // only 10 cases

switch() // only 10 cases
}
But with compiler optimizations would it make any difference? Would
one way be better than the other?

I'm using MSVC 6


Don't worry about efficiency. IMHO, the difference would be minimal. Go with what's
more readable and maintainable. Remember, either you or somebody else will probably
have to look at this a couple of years down the line, and if you make it easier to
deal with then, you've got a win.

Jul 19 '05 #5

"Rob Adelberg" <ro************ *@gd-decisionsystems .com> wrote in message news:f3******** *************** **@posting.goog le.com...
I have a single switch statement with about 30 case statements.

I could break the single switch into 3 or 4 switchs, which logically
would be more efficient.


That would probably not help. If the number of possible values versus
the number of cases is manageable, the compiler just generates a jump
table in most dcases.
Jul 19 '05 #6

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

Similar topics

26
14160
by: Joe Stevenson | last post by:
Hi all, I skimmed through the docs for Python, and I did not find anything like a case or switch statement. I assume there is one and that I just missed it. Can someone please point me to the appropriate document, or post an example? I don't relish the idea especially long if-else statements. Joe
10
10912
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a switch-case has a variable (most probably an enumeration) & associated symbols or integral value. Selection is made, base on what symbol/value the variable holds. So
15
7605
by: Mike and Jo | last post by:
I've been converting some code to C++. I'm trying to use the Switch function to compare a result. Is it possible to use switch to evaluate '>0', '<0', 0? Example switch (result) { case (>0): case (<0):
35
8360
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 for '5'. So in his switch statement, he omits a case for '5':
34
53442
by: Duncan McNutt [BSDM] | last post by:
Is it possible to have ranges or patterns in a case ? If not, why wasnt this designed in to make it easier instead of listing every case value needed? -- Duncan McNutt Microsoft Product Deactivation Team --
14
2576
by: Evan Camilleri | last post by:
I am going from VB.NET to c#. How can I do the following? switch x { case < 10: do something; break; case < 50: do something; break;
12
12355
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
2
1460
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name': 'fluffy', 'species': 'cat', 'size': 'small'} pets = {'name': 'bruno', 'species': 'snake', 'size': 'small'} pets = {'name': 'rex', 'species': 'dog', 'size': 'large'}
13
11854
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
0
9531
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
10237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
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
9055
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...
0
6795
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
5446
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...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2928
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.