Re: The IF statement....
Posted: Wed Jan 09, 2019 7:32 pm
wonderful that our beloved vic still generates discussion after all these yearsMike wrote:No worries!
At least CBM BASIC is still good for some surprises, isn't it?
The Commodore Vic 20 Forum
http://sleepingelephant.com/ipw-web/bulletin/bb/
http://sleepingelephant.com/ipw-web/bulletin/bb/viewtopic.php?t=8411
wonderful that our beloved vic still generates discussion after all these yearsMike wrote:No worries!
At least CBM BASIC is still good for some surprises, isn't it?
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 actMike wrote:They prove you *false*, to be exact.dr.geek wrote:those benchmarking tests counting jiffies are interesting
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.
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
different answer, fewer linesMike wrote: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?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....
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: