(a bit of long story)
Recently there was a task posted at LB forum
http://libertybasiccom.proboards.com/th ... -task-fast
Basically, it lead me to writing a program with long calculation involving MOD
I made some assumptions as to how to reduce search, and got right answer. (equal to with Rosetta code one)
After some thinking I got that idea that my seems-so-clever approach is not actually so:
I got number but I can't be sure that I checked all other numbers!
So I run it overnight, with single big loop (987654321, that much)
In the morning I discovered some error (like Virtual machine stack overflow)
and timestamp, so it run for 6 hours then crashed.
No other reason given.
(btw I recoded same program in Python - that is basic literally translated to Python, just after reading some first chapters in Python, yes - and it run overnight without no error.
But netbook I run it on fall asleep as I leaved it - so in the morning it finished but gave me total time 11 hours. I really think actual time was about 2 hours)
Anyway.
So I ran it on LBB and went away.
In the evening I saw program finished, right answer found, but total time is 9 hours!
I really expect LBB run faster - or my previous estimate for LB (6 hours) is grossly incorrect (as well could be).
Because - well - it's just FOR, WHILE, IF - nothing LB-specific? - should run much faster.
So I asked LBB to Options/show LBB pane
Whoa!
Code: Select all
if j mod 100000=0 then print ".";
Code: Select all
IF FN_mod(j, 100000) = 0 THEN PRINT FN_crlf(".");
(btw that means getting rid of these dots would sped up my code a lot)
So I went searching
Here's help for BBC basic MOD
http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin6.html
Indeed it works just for integers
LB does some extra stuff, emulating that requires a function
But the point is - just here I do not need extra stuff and would be happy with integer MOD!
So I tried using BBC code via "!"
something like
Code: Select all
!res=a mod b
Help at
http://lbbooster.com/lbb.html
on "! BBC code" says
So either assignment is not in this subset (that would really be a pity) or I am doing something wrongIf a line starts with an exclamation mark (!) the rest of the line is assumed to consist of BBC BASIC code and is passed, unmodified, to the output of the translator. This facility should be used with care since only a subset of BBC BASIC statements may be safely used.
(or rather doesn't know right "spell").
Ok so I went reading Help further.
Interesting one.%mode std
This directive causes LBB to evaluate the INT() function, and expressions containing both AND and OR operators, in a standard way; note that there must not be a space after the percent sign. Specifically the INT function truncates towards minus infinity (e.g. INT(−3.5) is −4) and OR has a lower precedence than AND (e.g. 1 OR 2 AND 4 is 1).
Difference between workings of INT - may be there is same for difference in MOD?
LBB pane shows that by default, INT() changes to FN_int()
but with "%mode std" directive it just stays INT(), that is, native BBC Basic.
Should be faster?
Let's see how much faster it goes
This code tries to check how much loops fits in a second
Code: Select all
'%mode std
N =1
tTreshold=1000
b=5
x=123
do
t0=time$("ms")
for i = 1 to N
a=int(x/b)
next
t1=time$("ms")
dt=t1-t0
N=N*1.1
loop until dt > tTreshold
print N
this first line commented
290960.772
this first line uncommented (native INT)
686070.281
Indeed, faster.
But alas "%mode std" does not affects MOD.
BUT
instead "x MOD b " I can use "x - b*INT(x/b)"
Let's compare using same program
"res = X MOD b "
290960.772
"res = x - b*INT(x/b)" , "%mode std" off
264509.793
"res = X - b*INT(X/b)" , "%mode std" on (native INT)
515454.757
so still I can make it run 75% faster
Now I'll go and change my long-running program...