Qbasic, let's face it, is a slow language. But there are 15 little often unknown tricks that can help speed your programs up
[color red]1)[/color] 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.
[color blue]FROM:[/color]
[color green] FOR b = 0 TO 1 STEP .1
a% = INT(a)
NEXT[/color]
[color blue]TO:[/color]
[color green] FOR b% = 0 TO 10
a% = b% / 10
NEXT[/color]
[color red]2)[/color] 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 PSET
[color blue]FROM:[/color]
[color green] 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$ " "[/color]
[color blue]TO:[/color]
[color green] 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 [/color]
[color red]3)[/color] Store the results of complicated algebra and trig in arrays
[color blue]FROM:[/color]
[color green] FOR a% = 1 to 360
a = a% * 180 / 3.1415926535
x = COS(a) * 100 + 100
y = SIN(a) * 100 + 100
PSET (x,y),10
NEXT
[/color]
[color blue]TO:[/color]
[color green] 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
NEXT
[/color]
[color red]4)[/color] 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.
[color blue]FROM:[/color]
[color green] FOR a% = 1 TO 10
FOR x% = 1 TO 10
aa% = sin(a%) * a% ^ 3 + x * 2
NEXT
NEXT
[/color]
[color blue]TO:[/color]
[color green] FOR a% = 1 To 10
suba% = sin(a%) * a% ^ 3
FOR x% = 1 TO 10
aa% = suba% + x * 2
NEXT
NEXT
[/color]
[color red]5)[/color] If a variable doesn't change, make it CONST
[color red]6)[/color] 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 speed
[color blue]FROM:[/color]
[color green] DO
a% = a% + 1
LOOP UNTIL a% = 12345
WHILE b% < 98765
b% = b% + 1
WEND
[/color]
[color blue]TO:[/color]
[color green] FOR a% = 1 TO 12345:NEXT
FOR b% = 1 TO 98765:NEXT
[/color]
[color red]7)[/color] 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 that
[color blue]FROM:[/color]
[color green] FOR a% = 10 to 20
PSET (15,a%), 14
NEXT
'The computer reads 33 commands
[/color]
[color blue]TO:[/color]
[color green] 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 commands
[/color]
[color red]8)[/color] Only use FOR-NEXT if you are running out of memory or the loop is extremely long, even then break it down
[color blue]FROM:[/color]
[color green] FOR a% = 1 to 500
PSET (a%,10), 14
NEXT
'The computer reads 1500 commands
[/color]
[color blue]TO:[/color]
[color green] 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 commands
[/color]
[color red]9)[/color] 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 RAM
[color blue]FROM:[/color]
[color green] DIM array%(100,100)
FOR x% = 1 TO 100
FOR y% = 1 TO 100
PSET (x%,y%), array%(x%,y%)
NEXT
NEXT
[/color]
[color blue]TO:[/color]
[color green] DIM array%(100,100)
FOR y% = 1 TO 100
FOR x% = 1 TO 100
PSET (x%,y%), array%(x%,y%)
NEXT
NEXT
[/color]
[color red]10)[/color] Use 1D arrays rather than 2D use DIM array%(10000) rather than array%(100,100)
[color red]11)[/color] Use BLOAD / BSAVE files instead of long loops of GET and PUT
[color red]12)[/color] Use SELECT CASE instead of IF-ELSEIF-END IF
[color red]13)[/color] Never compare against zero
[color blue]FROM:[/color]
[color green] IF a% <> 0 THEN PRINT "a% <> 0"[/color]
[color blue]TO:[/color]
[color green] IF a% THEN PRINT "a% <> 0"[/color]
[color red]14)[/color] 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 dramatically
[color red]15)[/color] Multiplication is up to five or six times faster than using powers. Use them to dramitically speed up programs with lots of powers.
[color blue]FROM:[/color]
[color green]a = b ^ 4[/color]
[color blue]TO:[/color]
[color green]a = b * b * b * b[/color]
[color red]16)[/color] 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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.