TIP: Click on subject to list as thread! ANSI
echo: power_bas
to: ALL
from: JAMSHID KHOSHRANGI
date: 1995-10-04 00:00:00
subject: PB Compiler Folding Flaw

$IF 0
    NOFOLD.BAS                                              NOFOLD.BAS
                            No-Fold Demonstration
    I've been rigorously investigating the PowerBASIC compiler and
    have come upon an unexpected fundamental flaw in the way the
    PowerBASIC compiler handles what is known as constant folding.
    In the interest of the education of PowerBASIC programmers who are
    looking to avoid major speed penalties if they're not careful, I
    am posting my findings in this echo.  Note: I am an avid fan of PB,
    not a critic, so these findings are just as disappointing to me
    as to anyone else.
    From Ethan Winer's book _BASIC Techniques and Utilities_, pages
    12-13:
        One important type of optimization is called _constant folding_.
        This means that as much math as possible is performed during
        compilation rather than when the program runs.  For example, if
        you have a statement such as X = 4 * Y * 3, BASIC changes it to
        X = Y * 12.  After all, why multiply 3 times 4 later when the
        answer can be determined now?  This substitution is performed
        entirely by the compiler, without your knowing about it.
    This code demonstrates that, to the PowerBASIC compiler at least,
    X = 4 * Y * 3 and X = Y * 12 are not one and the same.  Indeed, a
    simple 10*2 and 20 are not one and the same to the PB compiler....
    The solution?  Well, until the designers at PowerBASIC rectify the
    problem, you will have to fold constants by hand, writing 20 instead
    of 2 * 10, and most certainly writing 200 instead of 2 * 10 * 10!
    Or, perhaps someone who is also writing a preprocessor could design
    in constant folding at that level.
    Jamshid Khoshrangi
$ENDIF
DEFLNG A-Z
CLS
VIEW TEXT (23,5)-(70,20)
CLS
PRINT "Demonstration of lack of folding"
PRINT
PRINT "x = 4 * y * 3", "(";
MTIMER
FOR y = 1 TO 10000
    x = 4 * y * 3
NEXT i
PRINT MTIMER; ")"
PRINT "x = y * 12", "(";
MTIMER
FOR y = 1 TO 10000
    x = y * 12
NEXT i
PRINT MTIMER; ")"
PRINT
PRINT "x = 2 * 10", "(";
MTIMER
FOR y = 1 TO 10000
    x = 2 * 10
NEXT i
PRINT MTIMER ;")"
PRINT "x = 20", "(";
MTIMER
FOR y = 1 TO 10000
    x = 20
NEXT i
PRINT MTIMER; ")"
PRINT
PRINT "x = 2*10*10", "(";
MTIMER
FOR y = 1 TO 10000
    x = 2 * 10 * 10
NEXT i
PRINT MTIMER ;")"
PRINT "x = 200", "(";
MTIMER
FOR y = 1 TO 10000
    x = 200
NEXT i
PRINT MTIMER; ")"
 * OLX 2.1 TD * Customer Support: (n.) see PowerBASIC Inc. see also: fast
--- Maximus/2 2.01wb
---------------
* Origin: Sound Stage BBS - Live Via Satellite - (604)944-6476 (1:153/7070)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.