×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# QBasic FAQ

## optimization

 How can I make my program faster? by qbasicking faq314-3932 Posted: 24 Jul 03 (Edited 20 Aug 04) Qbasic, let's face it, is a slow language.  But there are 15 little often unknown tricks that can help speed your programs up1) Qbasic reads integers much faster than any other type of variable (because they are only 2 bytes), so use then whenever possible.  Qbasic also automatically converts longer variables to integers, so don't use the INT command, it just takes up time.FROM: FOR b = 0 TO 1 STEP .1   a% = INT(a) NEXTTO: FOR b% = 0 TO 10    a% = b% / 10 NEXT2) Use direct access commands (INP, OUT, PEEK, and POKE) whenever possible.  Use INP(96) instead of INKEY$, and OUT &h3c9 instead of PALETTE, PEEK instead of POINT and POKE instead of PSETFROM: SCREEN 13 FOR x% = 1 TO 100 FOR y% = 1 TO 100 PSET (x%, y%), POINT (x%, y%) + 1 NEXT NEXT DO: LOOP UNTIL INKEY$ " "TO: SCREEN 13 DEF SEG = &HA000 FOR y% = 1 TO 100   y2% = y% * 320   FOR x% = 1 TO 100     POKE (y2% + x%), PEEK (y2% + x%) + 1   NEXT NEXT DEF SEG DO: LOOP UNTIL INP(96) = 57   3) Store the results of complicated algebra and trig in arraysFROM: FOR a% = 1 to 360   a = a% * 180 / 3.1415926535   x = COS(a) * 100 + 100   y = SIN(a) * 100 + 100   PSET (x,y),10 NEXTTO: DIM circlex%(360), circley%(360) FOR a% = 1 TO 360   a = a% * 180 / 3.1415926535   circlex%(a%) = COS(a) * 100 + 100   circley%(a%) = SIN(a) * 100 + 100 NEXT ... FOR a% = 1 to 360   PSET (circlex%(a%), circley%(a%)),10 NEXT4)  If you have two or more FOR-NEXT loops, make all the calculations for one loop outside of the others, tough to understand, the example is pretty explainatory.FROM: FOR a% = 1 TO 10   FOR x% = 1 TO 10     aa% = sin(a%) * a% ^ 3 + x * 2   NEXT NEXTTO: FOR a% = 1 To 10   suba% = sin(a%) * a% ^ 3   FOR x% = 1 TO 10     aa% = suba% + x * 2   NEXT NEXT5) If a variable doesn't change, make it CONST6) Use FOR-NEXT instead of DO-LOOP or WHILE-WEND:  Qbasic moves much faster because it have to read less code.  Instead of 3 line (DO, calculation, LOOP) it only reads 2 (FOR and NEXT) making it 3/2 the speedFROM: DO   a% = a% + 1 LOOP UNTIL a% = 12345 WHILE b% < 98765   b% = b% + 1 WENDTO: FOR a% = 1 TO 12345:NEXT FOR b% = 1 TO 98765:NEXT7) People think making their code as short as possible increases speed - it does, but you have to make it shorter for the computer, not the user, heres what i mean.  In a FOR-NEXT loop every time it loops it read 3 commands (FOR, command, NEXT), minimize thatFROM: FOR a% = 10 to 20   PSET (15,a%), 14 NEXT     'The computer reads 33 commandsTO: PSET (15,10), 14    PSET (15,11), 14    PSET (15,12), 14    PSET (15,13), 14    PSET (15,14), 14    PSET (15,15), 14    PSET (15,16), 14    PSET (15,17), 14    PSET (15,18), 14    PSET (15,19), 14    PSET (15,20), 14 'The computer reads 11 commands8) Only use FOR-NEXT if you are running out of memory or the loop is extremely long, even then break it downFROM: FOR a% = 1 to 500   PSET (a%,10), 14 NEXT 'The computer reads 1500 commandsTO: FOR a% = 1 to 500 STEP 5   PSET (a%,10), 14   PSET (a% + 1,10), 14       PSET (a% + 2,10), 14   PSET (a% + 3,10), 14   PSET (a% + 4,10), 14 NEXT 'The computer reads 700 commands9) Qbasic stores 2D arrays in x variables across and y variables down, and the x's are in a scanline so read then from the scanline, rather than making qbasic jump around the RAMFROM: DIM array%(100,100) FOR x% = 1 TO 100   FOR y% = 1 TO 100     PSET (x%,y%), array%(x%,y%)   NEXT NEXT TO: DIM array%(100,100) FOR y% = 1 TO 100   FOR x% = 1 TO 100     PSET (x%,y%), array%(x%,y%)   NEXT NEXT10) Use 1D arrays rather than 2D use DIM array%(10000) rather than array%(100,100)11) Use BLOAD / BSAVE files instead of long loops of GET and PUT12) Use SELECT CASE instead of IF-ELSEIF-END IF13) Never compare against zeroFROM: IF a% <> 0 THEN PRINT "a% <> 0"TO: IF a% THEN PRINT "a% <> 0"14)  Compile it.  Qbasic emulates the .bas files into another language that DOS must then emulate, if DOS emulates its directly it speeds the program up dramatically15) Multiplication is up to five or six times faster than using powers.  Use them to dramitically speed up programs with lots of powers.FROM:a = b ^ 4TO:a = b * b * b * b16) If you have a multiplication calculation that you do over and over again, such as 320 * y + x for an offscreen buffer, convert it to asm.  Assembly has a bit shifting command, that eliminates multiplication, which is much slower.FROM:DEF SEG = VARSEG(buffer%(0))POKE VARPTR(buffer%(320 * y% + x% + 2)), c%...PUT (0,0), buffer%, PSETTO:CALL plotpixil(BYVAL(x%), BYVAL(y%), BYVAL(c%))...CALL copybuffer(BYVAL(buffer%(0)), BYVAL(A000));in library.model medium, basic.stack 200h.386.codepublic buffercopy, plotpixelbuffercopy proc    push  ds    push  bp    mov   bp, sp    mov   ds, [bp+10]    mov   es, [bp+8]    xor   si, si    xor   di, di    mov   cx, 32000    rep   movsw    pop   bp    pop   ds    ret   4buffercopy endpplotpixel proc    push  bp    mov   bp, sp    mov   es, [bp+12]    mov   dx, [bp+8]    mov   bx, dx    shl   dx, 8    shl   bx, 6    add   dx, bx    add   dx, [bp+10]    mov   di, dx    mov   al, [bp+6]    mov   es:[di], al    pop   bp    ret   8plotpixel endpend Back to QBasic FAQ Index Back to QBasic Forum

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!