Re: switch vs if

From: James Turner (turnerjh@XTN.NET)
Date: 05/03/98


--[[text/plain]]
Chris Powell <darkness@CHESHIRE.GATOR.NET> writes:

> WHich is quicker
> if (this == that || this == that2)
> or
> switch (this) {
>   case that:
>   case that2:

This isn't really the right question you should be asking... if speed
is an issue, worry more about the algorithm than whether the if or the
switch is faster.  Maybe post a piece of code that you're trying to
optimize for us to look at.  But you won't be gaining much speed in
these kinds of things -- they won't translate into very large
contructs (at most two comparisons and a jump).  You'd gain far more
speed in turning optimizations on and letting the compiler handle it.

<tirade>

Optimization is a very touchy thing.  In general, don't do it -- not
until you are completely done with everything else.  Pick a better
algorithm if necessary, but don't start hand-tweaking stuff like this
until the final stages of development.  Concentrate on quality code
until everything else is done.  Unless you are in a very tight loop,
don't worry about it, particularly not a case like the above -- go for
maintainable, robust code instead.

</tirade>

"Premature optimization is the root of all evil in programming."
(Attributed to Djikstra, C. A. R. Hoare, and Knuth, depending on what
source you look at).

Take the experts' advice.  Don't optimize until the very end, and only
then when if it is truly a problem.

Don't optimize for the processor or compiler, optimize for the
algorithm and the language.  The above question is much more an issue
of the former pair than the latter. :)

--
James Turner               turnerjh@xtn.net
                           http://www.vuse.vanderbilt.edu/~turnerjh/


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST