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 IF statement....
Moderator: Moderators
Re: The IF statement....
Re: The IF statement....
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.
-
- Vic 20 Afficionado
- Posts: 360
- Joined: Tue Apr 14, 2009 8:15 am
- Website: http://wimbasic.webs.com
- Location: Netherlands
- Occupation: farmer
Re: The IF statement....
Rewritten in WimBasic:
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.
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

And when lines 2-5 are taken together, and SuperNumbers (tm) are used, it's all done in 1275 ticks.
Last edited by wimoos on Thu Jan 24, 2019 10:14 am, edited 2 times in total.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
Re: The IF statement....
wasnt the trick to use DEF FN on the boolean expression? mmmh
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
- Kweepa
- Vic 20 Scientist
- Posts: 1303
- Joined: Fri Jan 04, 2008 5:11 pm
- Location: Austin, Texas
- Occupation: Game maker
Re: The IF statement....
That didn't seem to help, which isn't surprising since FNs aren't pre-interpreted.
-
- Vic 20 Hobbyist
- Posts: 108
- Joined: Sun Mar 10, 2019 7:39 pm
- Location: lodi california
- Occupation: student
Re: The IF statement....
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:
**** 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.
Bedroom coder=rock star
modern coder= pop star
i'd rather be a rock star than a pop start
modern coder= pop star
i'd rather be a rock star than a pop start
