Re: Processor Time

From: Eric L. Helvey (
Date: 07/12/96

Forgive me if my theory is a little off... it's been some time since I've
sat through a class on this but...

If my understanding is correct, the difference between nested if's, and
a multi-part if is neglible.  I believe the compiler translates both into
two comparison statements.  Where you would gain time is in the ordering of
the if's.  The idea is to "shortcut" out the statement by putting the
conditions that would fail most often first.  ie, if you had a situation 
where you wanted to distinguish male teenagers out of a group of random
people, you'd put the most discriminating condition (age 19 or less) first,
then the second most (age 13 or more), then the least (male)...  I believe
that is where you would gain your time.

I believe the same situation applies with the switch statement.  So that the
way you optimize for speed is to end the series of checks as soon as 
possible.  With switch, you want the case that will most often be true to
be first so that it doesn't have to run each check as you go down the list.

If I'm wrong, (and I may be... long time since I've been in a classroom) 
lemme know, but that's my understanding of it.


On Fri, 12 Jul 1996, Brian Christopher Guilbault wrote:

> Hiyaz-
> This question kind of goes out to you CS students or those who know a bit 
> about theory I guess. I was wondering which is more processor efficient, 
> a nested "if" or a single if composed of multiple checks. For example:
>   if (GET_LEVEL(ch) >=15)
>     perform_this_function(ch);
> vs.
> if (GET_CLASS(ch) == CLASS_WARRIOR && GET_LEVEL(ch) >= 15)
>   perform_this_function(ch); 
> I am of the opinion that a switch is more efficient than a bunch of "else 
> if's" as well. The reason I ask is because my code, as well as the stock 
> circle has A LOT of each of these. I was thinking about going through and 
> (a little at a time) changing these. I know that the time difference in 
> these is really small, but perhaps there is a long term benefit?
> BTW, I did check my C books and couldn't find anything that would answer 
> this question.
> Thanks,
>   -Brian
> -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> Brian Guilbault - GMI Engineering Institute
> E-mail:,
> WWW:
> QuarantineMUD: Telnet to 4000
> -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

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