Page 3 of 3

Re: The IF statement....

Posted: Wed Jan 09, 2019 7:32 pm
by dr.geek
Mike wrote:No worries! :)

At least CBM BASIC is still good for some surprises, isn't it? :mrgreen:
wonderful that our beloved vic still generates discussion after all these years

Re: The IF statement....

Posted: Thu Jan 10, 2019 1:48 am
by dr.geek
Mike wrote:
dr.geek wrote:those benchmarking tests counting jiffies are interesting
They prove you *false*, to be exact.

When your game main loops became faster, this was likely because of general optimisations like using less program lines overall, eliminating superfluous instructions, storing often used constants in 'early' variables, arranging the GOTO/GOSUB target lines for minimal search time, etc. But not by the replacement of IF statements by Boolean expressions.
Mike, seems i owe you an apology....the more complex the boolean statement, the more calculations causing a greater speed loss....I would say each case is its own challenge to find the optimal solution such as look-up tables to save speed at the cost of a few more bytes of memory....the eternal balancing act

Re: The IF statement....

Posted: Thu Jan 10, 2019 3:02 am
by wimoos
Rewritten in WimBasic:

Code: Select all

1 A=TI
2 FOR I=1 TO 1000
3 X=RND(8)
4 S=S-ROUND(X/2)*(1+2*ODD(X))
5 NEXT
6 PRINT TI-A
Results in: 624 (line 3) +730 (line 4)=1354 ticks (better than CBM Basic) and a pretty readable Basic program :-)
And when lines 2-5 are taken together, and SuperNumbers (tm) are used, it's all done in 1275 ticks.

Re: The IF statement....

Posted: Thu Jan 10, 2019 3:53 am
by groepaz
wasnt the trick to use DEF FN on the boolean expression? mmmh

Re: The IF statement....

Posted: Thu Jan 10, 2019 10:10 am
by Kweepa
That didn't seem to help, which isn't surprising since FNs aren't pre-interpreted.

Re: The IF statement....

Posted: Thu Mar 14, 2019 12:25 am
by 20questions
Mike wrote:
dr.geek wrote:with a bit of planning, almost all IF statements can be replaced with (multiple) boolean statements.

eg.
if x = 1 then p=p+1
if x = 2 then p=p-1

can be achieved with

p=p+(x=2)-(x=1)

much faster....
Have you ever made own time measurements, or are you just repeating the urban myth of any substantial speed improvement of Boolean expressions over IF statements?
screen_1.png
screen_2.png
More compact? Yes, probably. *Much* faster? No.

Here, the Boolean expression is slightly faster (just 29 jiffies per 1000 evaluations!). With more complex expressions, the Boolean expression is likely to become slower than multiple-line IF statements, as the comparisons have all to be evaluated anyway, but the IF version only needs to execute one single THEN statement, whereas the Boolean expression needs to evaluate all subexpressions in full:
screen_3.png
screen_4.png
different answer, fewer lines

**** cbm basic v2 ****

3583 bytes free

ready.
1 t1=t1:fort=1to1000
2 x=int(rnd(1)*2)+1:if
x=1thenp=p+1:ifx=2then
p=p-1:next
3 t2=ti:printt2-t1
run
67421

ready.