Re: [OT] Switch question
From: Alan Balmer (albalmer_at_att.net)
Date: 02/23/04
- Next message: Francois Grieu: "Re: value of ((int)0.7) and ((int)-0.7)"
- Previous message: those who know me have no need of my name: "Re: tmpnam question"
- In reply to: Sean Kenwrick: "[OT] Switch question"
- Next in thread: Villy Kruse: "Re: [OT] Switch question"
- Reply: Villy Kruse: "Re: [OT] Switch question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Francois Grieu: "Re: value of ((int)0.7) and ((int)-0.7)"
- Previous message: those who know me have no need of my name: "Re: tmpnam question"
- In reply to: Sean Kenwrick: "[OT] Switch question"
- Next in thread: Villy Kruse: "Re: [OT] Switch question"
- Reply: Villy Kruse: "Re: [OT] Switch question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|