Bogs: Even though you don't care about speed for this app, perhaps much faster code would be interesting or illuminating.
Some feel the need for speed. Hermann's nBits presents a good opportunity for eliminating a "repeat for each line" statement.
Here's a version that produces the same output as nBits, but does it about
eight times as fast on my machine.
Code: Select all
function nBits n,t -- returns the 2^n possible n-bit nums -- from Hermann
local s
if n=1 then return t
repeat for each line L in t
put cr & L & "0" after s
put cr & L & "1" after s
end repeat
return nBits(n-1,char 2 to -1 of s)
end nBits
function nBits2 pLength -- advantages: no "repeat for each line" and no recursion
local tStrings
put cr & "0" & cr & "1" into tStrings
repeat with i = 2 to pLength
replace cr with cr & 0 in tStrings -- put 0 before each line
get tStrings
replace cr & 0 with cr & 1 in it -- replace each leading 0 with 1
put it after tStrings
end repeat
delete char 1 of tStrings
return tStrings
end nBits2
on mouseUp
set cursor to watch
local tMilliseconds, tStrings, tReport
repeat with tLength = 2 to 24
put - the milliseconds into tMilliseconds[ "nBits" ]
put nBits( tLength, "0" & cr & "1" ) into tStrings[ "nBits" ]
add the milliseconds to tMilliseconds[ "nBits" ]
put - the milliseconds into tMilliseconds[ "nBits2" ]
put nBits2( tLength ) into tStrings[ "nBits2" ]
add the milliseconds to tMilliseconds[ "nBits2" ]
if tMilliseconds[ "nBits" ] > 5 and tMilliseconds[ "nBits2" ] > 0 then
get tLength \
&& ( number of lines in tStrings[ "nBits" ] ) \
&& ( tStrings[ "nBits" ] = tStrings[ "nBits2" ] ) \
&& tMilliseconds[ "nBits" ] \
&& tMilliseconds[ "nBits2" ] \
&& round( ( tMilliseconds[ "nBits2" ] - tMilliseconds[ "nBits" ] ) * 100 / tMilliseconds[ "nBits" ] ) & "%" \
& cr
put it after msg
put it after tReport
wait 0 with messages
end if
end repeat
if tReport is not empty then
replace space with comma in tReport
put "length,count,nBits=nBits2,nBits ms,nBits2 ms,delta" & cr before tReport
end if
breakpoint
end mouseUp
Here's the report on my 2017 iMac 3.8GHz with LC Indy 9.5.0.
Code: Select all
length,count,nBits=nBits2,nBits ms,nBits2 ms,delta
11,2048,true,7,1,-86%
12,4096,true,16,1,-94%
13,8192,true,31,2,-94%
14,16384,true,61,7,-89%
15,32768,true,141,10,-93%
16,65536,true,276,19,-93%
17,131072,true,590,48,-92%
18,262144,true,1192,122,-90%
19,524288,true,1679,161,-90%
20,1048576,true,3872,534,-86%
21,2097152,true,7767,1060,-86%
22,4194304,true,15950,1563,-90%
23,8388608,true,31288,4428,-86%
24,16777216,true,63586,7792,-88%
Thanks, all, for the discussion.
-- Dick