Re: switch vs if

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

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.


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.


"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     

     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | |

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