Ternary operator in ZX81 BASIC

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
MOB-i-L
Posts: 62
Joined: Mon Apr 19, 2010 12:13 am
Location: Lund, Skåne/Scania, Sweden
Contact:

Ternary operator in ZX81 BASIC

Post by MOB-i-L »

I have tried to make the equivalent of C's ternary operator in ZX81 BASIC.
B-(B-A AND L) was the most effective replacement I could find for L?A:B in C. Could it be improved?
Image
http://www.df.lth.se.orbin.se/~mikaelb/ ... /ternary.p
Last edited by MOB-i-L on Sat Oct 26, 2013 9:53 am, edited 1 time in total.
User avatar
Andy Rea
Posts: 1606
Joined: Fri May 09, 2008 2:48 pm
Location: Planet Earth
Contact:

Re: Ternary operator in ZX81 BASIC

Post by Andy Rea »

what about...

(A and L) + (B and not L)

Could do with running some speed tests, in the ZX81 shorter is not always quicker where math is concerned.... a single adding + 2 boolean i think is very slightly quicker then 2 subs and a single boolean.

Regards Andy
what's that Smell.... smells like fresh flux and solder fumes...
User avatar
Paul
Posts: 1511
Joined: Thu May 27, 2010 8:15 am
Location: Germanys west end

Re: Ternary operator in ZX81 BASIC

Post by Paul »

I did some tests with eo 50Hz slow mode.
100 loops with your

Code: Select all

30 LET C=B-(B-A AND L) 
took 4,08 seconds when L=0 and 4,24 seconds when L=1
fastest I found was

Code: Select all

30 LET C=B
40 IF L THEN LET C=A
took 3,36 seconds when L=0 and 3,94 seconds when L=1
all other examples were even slower.

Any better idea?
Paul
ternaryP.P
GOTO 1000 for speedtest
(1.33 KiB) Downloaded 229 times
In theory, there is no difference between theory and practice. But, in practice, there is.
User avatar
stefano
Posts: 542
Joined: Tue Dec 11, 2012 9:24 am
Contact:

Re: Ternary operator in ZX81 BASIC

Post by stefano »

Good point, the expression evaluation involves the internal stack based FP calculator which is compact and powerful but damn slow.
i.e. compare the time it takes printing a constant (like PRINT 1) and a text string (as PRINT "1").

Your suggestion permits little more optimizations, in example, your speed test loop could be changed like this:

Code: Select all

30 LET C=B
40 IF L THEN NEXT---
50 LET C=A
60 NEXT --
Post Reply