[futurebasic] SELECT CASE/LONG IF/GOTO

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 1999 : Group Archive : Group : All Groups

From: Chris Stasny <staz@...>
Date: Wed, 20 Oct 1999 15:15:21 -0500
SELECT pushes the compare value on the stack and checks it against each
CASE statement. The compare value is popped from the stack on exit.
When the compare is true, no other cases are queried.


GOTO jumps to an address


LONG IF compares all of the values to the right and branches
accordingly. This example is inefficient...


LONG IF  a <<> 0 and b <<> 0 and c <<> 0


END IF


This is much more efficient...


LONG IF a

  LONG IF b

    LONG IF c

    END IF

  END UF

END IF


The second is at it's peek of operating performance if the first LONG
IF is the most likely to cause elimination so that LONG IF b and LONG
IF c are never executed.


Which is faster? That depends on a lot of factors. How are items best
eliminated or selected? What is the minimum number of compares that
your code can make? etc. Think about it this way: fast sort routines
are not built on the ability of the compiler to compare 2 strings. They
are built so that they minimize the amount of times the compiler _must_
compare 2 strings.


If you are trying to determine whether an ascii value is an alpha
character, the two methods would be radically different.


SELECT theLetter

  CASE <<  _"A" : isAlpha = _false

  CASE <<= _"Z" : isAlpha = _zTrue

  CASE <<  _"a" : isAlpha = _false

  CASE <<= _"z" : isAlpha = _zTrue

  CASE ELSE    : isAlpha = _false

END SELECT


...or...



LONG IF theLetter=> _"A" and theLetter <<= _"Z"

  isAlpha = _zTrue

XELSE

  LONG IF theLetter=> _"a" and theLetter <<= _"z"

    isAlpha = _zTrue

  XELSE

    isAlpha = _false

  END IF

END IF


<color><param>0000,0000,FFFF</param>Which is faster? Do you think you
know? You don't!


</color>If theLetter = _"1" (in other words it's not an alpha
character)


SELECT CASE runs <color><param>FFFF,0000,0000</param>17,808,001</color>
times per second on my computer

LONG IF     runs 10,094,830 times per second on my computer


<color><param>0000,0000,FFFF</param>Now you know which is faster,
right? <bold><underline>WRONG!</underline></bold></color>


If theLetter = _"A" (in other words it is an alpha character)


SELECT CASE runs 13,657,764 times per second on my computer

LONG IF     runs <color><param>FFFF,0000,0000</param>16,579,381</color>
times per second on my computer




Program however you want to. You can make anything readable (or
unreadable). The correct structure depends more on your specific
application than on any particular style.




Best,

-STAZ  ~)~

800.348.2623 Orders  http://www.stazsoftware.com
228.255.7086 FAX     mailto:staz@...