valueDiff for arrays?

Alex Tweedly alex at tweedly.net
Mon Aug 6 16:04:21 EDT 2018


On 06/08/2018 16:50, Mark Waddingham via use-livecode wrote:

> Alex Tweedly didn't talk nonsense... Byte x [to y] of z is (truly) constant time if z is strictly a binary string.
That's right - the basic principle wasn't nonsense - but most everything 
else in my email was :-)

I said you only needed to change two lines - in fact you need to make 
changes in 4 places.
I said the saving was ~ 40%, in fact it's ~75-80%

First wrong thing - you can't just casually write into  "byte X of tmp". 
if X is greater than the current length of tmp, then this simply appends 
a single byte. So you need to pre-fill the variable that holds the bytes.

Second wrong thing was
   put "a" into byte X of np
that should be
   put numtobyte(66) into byte X of np

I don't thin this *should* make much difference - but in fact it makes a 
huge difference. Not sure why - I suspect a bug ,,,, I'll look at it 
some more and hopefully submit a bug report later.

Third wrong thing -  I wasn't properly calculating latter set of primes.

Fourth wrong thing - I was running the wrong test stack :-)

Fifth additional minor tweak - there's no need for the array 'p'; all it 
was used for was extracting the keys, sorting them and using that to 
start the textual list of results. Better to just put the confirmed 
primes into the textual list.

So - fixing all those gives code that runs about 75-80% faster. See 
revised code below.

Now off to try a different, perhaps crazy, idea :-)

Code - sorry about the formatting ...


on mouseUp

local T

put the long seconds into T

repeat with c = 1 to 2

get get_primes(10000000)

end repeat

put "button Found" && the number of lines in it && "primes in" && the 
long seconds - T && "seconds" &CR after fld "F"

end mouseUp

function get_primes n

local mroot, np, R

repeat with i = 1 to n

put numtobyte(66) into byte i of np

end repeat

if n < 2 then return empty

if n = 2 then return 2

put trunc(sqrt(n)) - 1 into mroot

repeat with i = 3 to mroot step 2

if byte i of np = "a" then next repeat

-- put TRUE into p[i]

put i &CR after R

repeat with j = i*i to n step i

put numtobyte(65) into byte j of np

end repeat

end repeat

-- put 2 & cr & the keys of p into R

-- sort lines of R numeric

repeat with i = mroot + (mroot + 1) mod 2 to n - 1 step 2

if byte i of np = numtobyte(66) then put i & CR after R

end repeat

return R

end get_primes


-- Alex.




More information about the use-livecode mailing list