The IF statement....

Basic and Machine Language

Moderator: Moderators

dr.geek
Vic 20 Drifter
Posts: 22
Joined: Tue Aug 28, 2018 5:05 am

Re: The IF statement....

Post 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
dr.geek
Vic 20 Drifter
Posts: 22
Joined: Tue Aug 28, 2018 5:05 am

Re: The IF statement....

Post 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
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: The IF statement....

Post 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.
Last edited by wimoos on Thu Jan 24, 2019 10:14 am, edited 2 times in total.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
groepaz
Vic 20 Scientist
Posts: 1187
Joined: Wed Aug 25, 2010 5:30 pm

Re: The IF statement....

Post by groepaz »

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.
User avatar
Kweepa
Vic 20 Scientist
Posts: 1315
Joined: Fri Jan 04, 2008 5:11 pm
Location: Austin, Texas
Occupation: Game maker

Re: The IF statement....

Post by Kweepa »

That didn't seem to help, which isn't surprising since FNs aren't pre-interpreted.
20questions
Vic 20 Hobbyist
Posts: 114
Joined: Sun Mar 10, 2019 7:39 pm
Location: lodi california
Occupation: student

Re: The IF statement....

Post 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.
Bedroom coder=rock star
modern coder= pop star
i'd rather be a rock star than a pop start 8)
Post Reply