Re: [OT] Switch question

From: Alan Balmer (albalmer_at_att.net)
Date: 02/23/04


Date: Mon, 23 Feb 2004 11:28:37 -0700

On Sat, 21 Feb 2004 11:30:13 +0000 (UTC), "Sean Kenwrick"
<skenwrick@hotmail.com> wrote:

>I have a large switch statement (~50 cases ) inside a critical inner loop of
>a byte code interpreter I am working on, and I am wondering if I should take
>any steps to optimise this loop.

How would we know ? <G>. Is it running too slow? Have you profiled the
program and determined that the switch statement in this loop is using
a significant amount of time? If you haven't done this, you're not
ready to start optimizing.
>
>Are switch statements usually compiled as if...else if..else if...else type
>statements?
Very unlikely.

> Are there other ways that the compiler could implement switch
>statements that are more efficient?
Yes - several ways. The compiler may choose different ways depending
on its analysis of your code.
>
>With regards to my code, would it be better to put the most common cases
>near the top of the switch statement
Probably a waste of time, and may make the code less readable.

> or should I consider making a lookup
>table of function pointers instead (but the function call overhead might
>kill it) - this is the first time that I wish I could put labels into arrays
>and do 'goto label[x]' :-) )
No. This would be restricting the compiler's choices. As for "goto
label[x]', the compiler may choose to do it that way.
>
>I've tried posting to comp.compilers but nothing has appeared in this group
>for iver a week and it seems to be dead ....
>
>Any advice, hints etc would be appreciated...
>
>Sean
>
Profile first. Profile after each change. Look for better algorithms
before worrying about code details. Study the compiler's output - you
may be surprised.

-- 
Al Balmer
Balmer Consulting
removebalmerconsultingthis@att.net


Relevant Pages

  • Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the
    ... Alphabetic for strings isn't quite so ... One example of where people go wrong is when they want to optimise loop ... implementation so that each iteration takes 10% less time will only ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Loading an I/O intensive program into memory
    ... explicitly loading the lot into RAM. ... You can lock programs into memory, but that may or may not help. ... It's easier to redo the main loop of code in shell than in C. ... Run your program with real input to gather the profile data. ...
    (comp.os.linux.misc)
  • Re: regarding "goto" in C
    ... finite state machine, where a goto actually models something in the problem domain (this could also be implemented as a switch statement in a loop with an explicit state variable); error handling; and breaking out of a ... it causes problems if an inner loop accesses variables ...
    (comp.lang.c)
  • Re: for loop faster than vectorized
    ... ML tries best to look at code and optimise it for you, ... It might throw off the internal MATLAB 'tweaker' and you would ... Say do a rand inside the loop: ... when using small vectors and running the calculation more ...
    (comp.soft-sys.matlab)
  • Re: Calling a subroutine causes serialization of threads?
    ... I didn't expect to get any response so quickly ... "It would be interesting to change the loop count to see if you can ... Or profile the code using a high resolution ... timer or a hardware performance counter to see ...
    (comp.programming.threads)