FB II Compiler

PG PRO

Debugging

Memory

System

Mathematics

Resources

Disk I/O

Windows

Controls

Menus

Mouse

Keyboard

Text

Fonts

Drawing

Sound

Clipboard

Printing

Communication

ASM

Made with FB

FB II COMPILER

Consider speed with conditional structures (III)


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 IF
END IF
The second is at it's peak 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
Which is faster? Do you think you know? You don't!

If theLetter = _"1" (in other words it's not an alphacharacter)

SELECT CASE runs 17,808,001 times per second on my computer
LONG IF runs 10,094,830 times per second on my computer

Now you know which is faster, right? WRONG!

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 16,579,381 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.
STAZ