Re: QL Tinkering
Posted: Fri Sep 21, 2018 5:29 pm
Hi, WRAP I have some further checks and interesting results on composite AND as opposed to nested IF statements. As to FOR loops my Iteration Test prog results show as you implied For loop using integers are slightly faster maybe.
Ref; New chapter for SBASIC/SuperBASIC Reference Manual Norman here’s a possible part two...
SuperBASIC Interpreter Timings
A number of factors in executing elements of a program have already been suggested by the way the Interpreter acts on the code elements and syntax of each line. Prioritising the namelist is one, how keywords process variables is another.
‘IF’ expressions and how they are handled by the Interpreter in execution, can nested IF expressions be executed faster by the interpreter than processing multiple expressions using combination logical operators.
For example nested IFs: IF a<b :IF c<d THEN if both are true the following statement is executed. This can also be written: IF a<b AND c<d THEN again if both are true the following statement is executed. The following Program carries out a number of iterations to determine the SuperBASIC Interpreters speed of processing certain actions. The First half deals with the IF expressions:- Procedure Test01
REPeat loops run for both nested IF and combination logical AND expressions. Then run again for a second time but in reverse order to compensate any advantage of one loop running before the other. By altering the Value on line number 130,142, 154,166 the set trial time can be extended. However be sure to compensate for the full amount of time by changing the values following the commands PRINT +5 etc.
100 REMark SBTimeTrial (SuperBASIC Iteration Timer)
102 :
104 WINDOW#2,512,200,0,0:PAPER#2,0:CLS#2
106 WINDOW 400,84,20,12:PAPER 0:BORDER 2,2:INK 7:Test01
108 :
110 DEFine PROCedure Test01
112 LOCal a,b,c,d,n,st,ts,sw$:a=5:b=3:c=3:d=5
114 CSIZE 2,1:CURSOR 10,2:PRINT 'IF a>b:IF c<d Test'
116 CURSOR 10,40:PRINT 'IF a>b AND c<d Test':CSIZE 0,0
118 CURSOR 10,24:PRINT 'Iterations: ':CURSOR 200,24:PRINT 'Stopwatch: '
120 CURSOR 10,64:PRINT 'Iterations: ':CURSOR 200,64:PRINT 'Stopwatch: '
122 n=0:st=DATE
124 REPeat lp
126 IF a>b:IF c<d:n=n+1
128 CURSOR 88,24:PRINT n:CURSOR 266,24
130 IF DATE-st=5:PRINT 5:EXIT lp:ELSE PRINT DATE-st
132 END REPeat lp
134 n=0:st=DATE
136 REPeat lp
138 IF a>b AND c<d:n=n+1
140 CURSOR 88,64:PRINT n:CURSOR 266,64
142 IF DATE-st=5:PRINT 5:EXIT lp:ELSE PRINT DATE-st
144 END REPeat lp
146 n=0:st=DATE
148 REPeat lp
150 IF a>b AND c<d:n=n+1
152 CURSOR 140,64:PRINT n:CURSOR 266,64
154 IF DATE-st=5:PRINT 10:EXIT lp:ELSE PRINT 5+DATE-st
156 END REPeat lp
158 n=0:st=DATE
160 REPeat lp
162 IF a>b:IF c<d:n=n+1
164 CURSOR 140,24:PRINT n:CURSOR 266,24
166 IF DATE-st=5:PRINT 10:EXIT lp:ELSE PRINT 5+DATE-st
168 END REPeat lp
170 END DEFine
172 :
The second half of the Program redraws the output window for the Procedure Test02 output. This looks at the FOR loop executing Integer variables as opposed to Floating Point variables. Variables suffixed with ‘a%’ are identified as Integer variables, variables held by ‘a’ will be treated as a Floating Point. REPeat loops run for both Integer and Floating Point FOR loop then run a second time but in reverse order to compensate any advantage one may hold over the other. By altering the Value on line number 200,216, 232,248 the trial time can be reduced. The reason being emulators such as QPC2 running on today’s faster computers will run into overflow with the limit of Integer numbers (-32768 to +32767). However be sure to change the time values that follow the PRINT commands etc. in line with IF DATE-st=4.
174 WINDOW 400,84,20,100:BORDER 2,2:Test02
176 :
178 DEFine PROCedure Test02
180 LOCal n1%,n12,ts,st,ta
182 CSIZE 2,1:CURSOR 10,2:PRINT 'For loop Integer Test'
184 CURSOR 10,40:PRINT 'For loop Floating Point Test':CSIZE 0,0
186 CURSOR 10,24:PRINT 'Iterations: ':CURSOR 200,24:PRINT 'Stopwatch: '
188 CURSOR 10,64:PRINT 'Iterations: ':CURSOR 200,64:PRINT 'Stopwatch: '
190 n1%=0:st=DATE
192 REPeat lp
194 FOR i=1 TO 20000
196 n1%=n1%+1
198 CURSOR 88,24:PRINT n1%:CURSOR 266,24
200 IF DATE-st=4:PRINT 4:EXIT lp:ELSE PRINT DATE-st
202 END FOR i
204 END REPeat lp
206 n12=0:st=DATE
208 REPeat lp
210 FOR i=1 TO 20000
212 n12=n12+1
214 CURSOR 88,64:PRINT n12:CURSOR 266,64
216 IF DATE-st=4:PRINT 4:EXIT lp:ELSE PRINT DATE-st
218 END FOR i
220 END REPeat lp
222 n12=0:st=DATE
224 REPeat lp
226 FOR i=1 TO 20000
228 n12=n12+1
230 CURSOR 140,64:PRINT n12:CURSOR 266,64
232 IF DATE-st=4:PRINT 8:EXIT lp:ELSE PRINT 4+DATE-st
234 END FOR i
236 END REPeat lp
238 n1%=0:st=DATE
240 REPeat lp
242 FOR i=1 TO 20000
244 n1%=n1%+1
246 CURSOR 140,24:PRINT n1%:CURSOR 266,24
248 IF DATE-st=4:PRINT 8:EXIT lp:ELSE PRINT 4+DATE-st
250 END FOR i
252 END REPeat lp
254 END DEFine
The results:- ???
QBITS
Ref; New chapter for SBASIC/SuperBASIC Reference Manual Norman here’s a possible part two...
SuperBASIC Interpreter Timings
A number of factors in executing elements of a program have already been suggested by the way the Interpreter acts on the code elements and syntax of each line. Prioritising the namelist is one, how keywords process variables is another.
‘IF’ expressions and how they are handled by the Interpreter in execution, can nested IF expressions be executed faster by the interpreter than processing multiple expressions using combination logical operators.
For example nested IFs: IF a<b :IF c<d THEN if both are true the following statement is executed. This can also be written: IF a<b AND c<d THEN again if both are true the following statement is executed. The following Program carries out a number of iterations to determine the SuperBASIC Interpreters speed of processing certain actions. The First half deals with the IF expressions:- Procedure Test01
REPeat loops run for both nested IF and combination logical AND expressions. Then run again for a second time but in reverse order to compensate any advantage of one loop running before the other. By altering the Value on line number 130,142, 154,166 the set trial time can be extended. However be sure to compensate for the full amount of time by changing the values following the commands PRINT +5 etc.
100 REMark SBTimeTrial (SuperBASIC Iteration Timer)
102 :
104 WINDOW#2,512,200,0,0:PAPER#2,0:CLS#2
106 WINDOW 400,84,20,12:PAPER 0:BORDER 2,2:INK 7:Test01
108 :
110 DEFine PROCedure Test01
112 LOCal a,b,c,d,n,st,ts,sw$:a=5:b=3:c=3:d=5
114 CSIZE 2,1:CURSOR 10,2:PRINT 'IF a>b:IF c<d Test'
116 CURSOR 10,40:PRINT 'IF a>b AND c<d Test':CSIZE 0,0
118 CURSOR 10,24:PRINT 'Iterations: ':CURSOR 200,24:PRINT 'Stopwatch: '
120 CURSOR 10,64:PRINT 'Iterations: ':CURSOR 200,64:PRINT 'Stopwatch: '
122 n=0:st=DATE
124 REPeat lp
126 IF a>b:IF c<d:n=n+1
128 CURSOR 88,24:PRINT n:CURSOR 266,24
130 IF DATE-st=5:PRINT 5:EXIT lp:ELSE PRINT DATE-st
132 END REPeat lp
134 n=0:st=DATE
136 REPeat lp
138 IF a>b AND c<d:n=n+1
140 CURSOR 88,64:PRINT n:CURSOR 266,64
142 IF DATE-st=5:PRINT 5:EXIT lp:ELSE PRINT DATE-st
144 END REPeat lp
146 n=0:st=DATE
148 REPeat lp
150 IF a>b AND c<d:n=n+1
152 CURSOR 140,64:PRINT n:CURSOR 266,64
154 IF DATE-st=5:PRINT 10:EXIT lp:ELSE PRINT 5+DATE-st
156 END REPeat lp
158 n=0:st=DATE
160 REPeat lp
162 IF a>b:IF c<d:n=n+1
164 CURSOR 140,24:PRINT n:CURSOR 266,24
166 IF DATE-st=5:PRINT 10:EXIT lp:ELSE PRINT 5+DATE-st
168 END REPeat lp
170 END DEFine
172 :
The second half of the Program redraws the output window for the Procedure Test02 output. This looks at the FOR loop executing Integer variables as opposed to Floating Point variables. Variables suffixed with ‘a%’ are identified as Integer variables, variables held by ‘a’ will be treated as a Floating Point. REPeat loops run for both Integer and Floating Point FOR loop then run a second time but in reverse order to compensate any advantage one may hold over the other. By altering the Value on line number 200,216, 232,248 the trial time can be reduced. The reason being emulators such as QPC2 running on today’s faster computers will run into overflow with the limit of Integer numbers (-32768 to +32767). However be sure to change the time values that follow the PRINT commands etc. in line with IF DATE-st=4.
174 WINDOW 400,84,20,100:BORDER 2,2:Test02
176 :
178 DEFine PROCedure Test02
180 LOCal n1%,n12,ts,st,ta
182 CSIZE 2,1:CURSOR 10,2:PRINT 'For loop Integer Test'
184 CURSOR 10,40:PRINT 'For loop Floating Point Test':CSIZE 0,0
186 CURSOR 10,24:PRINT 'Iterations: ':CURSOR 200,24:PRINT 'Stopwatch: '
188 CURSOR 10,64:PRINT 'Iterations: ':CURSOR 200,64:PRINT 'Stopwatch: '
190 n1%=0:st=DATE
192 REPeat lp
194 FOR i=1 TO 20000
196 n1%=n1%+1
198 CURSOR 88,24:PRINT n1%:CURSOR 266,24
200 IF DATE-st=4:PRINT 4:EXIT lp:ELSE PRINT DATE-st
202 END FOR i
204 END REPeat lp
206 n12=0:st=DATE
208 REPeat lp
210 FOR i=1 TO 20000
212 n12=n12+1
214 CURSOR 88,64:PRINT n12:CURSOR 266,64
216 IF DATE-st=4:PRINT 4:EXIT lp:ELSE PRINT DATE-st
218 END FOR i
220 END REPeat lp
222 n12=0:st=DATE
224 REPeat lp
226 FOR i=1 TO 20000
228 n12=n12+1
230 CURSOR 140,64:PRINT n12:CURSOR 266,64
232 IF DATE-st=4:PRINT 8:EXIT lp:ELSE PRINT 4+DATE-st
234 END FOR i
236 END REPeat lp
238 n1%=0:st=DATE
240 REPeat lp
242 FOR i=1 TO 20000
244 n1%=n1%+1
246 CURSOR 140,24:PRINT n1%:CURSOR 266,24
248 IF DATE-st=4:PRINT 8:EXIT lp:ELSE PRINT 4+DATE-st
250 END FOR i
252 END REPeat lp
254 END DEFine
The results:- ???
QBITS