Relative Speed of different loop structures. [was Finding non-common elements]
Buster
wouter.abraham at scarlet.be
Mon Nov 7 22:44:03 EST 2005
On 06 Nov 2005, at 03:48, Alex Tweedly wrote:
-snip completely-
Hi,
I was wondering why your code wasn't working correctly.
Because I liked your idea and wanted to test its speed, I delved in
and found 2 problems
- using the < (or >) operator in this case must be done on
literals and not on numerics.
--> a step must be added to make sure both lists are literals
- to prevent an endless loop: last line of t2 >= last line of t1
Here it is in working/mint condition:
on mouseup
put fld "Field" into t1 ### & cr & "ZZZZZZZZZZ" into t1
put fld "Field" into t2 ### & cr & "test line" & cr &
"ZZZZZZZZZZ" into t2
put the millisecs into tStart
put 1 into i2
put the number of lines in t2 into limit2
### making sure the strings are non numerics
if char -1 of t1 is not cr then put cr after t1
if char -1 of t2 is not cr then put cr after t2
replace cr with " a"&cr in t1
replace cr with " a"&cr in t2
sort t1
sort t2
### swap if necessary
if line -1 of t1 > line -1 of t2 then
put t1 into a
put t2 into t1
put a into t2
end if
split t2 by CR
put t2[1] into L2
repeat for each line L1 in t1
repeat while L2 < L1
add 1 to i2
put t2[i2] into L2
end repeat
if L2 = L1 then
-- put L1 & cr after tBoth
add 1 to i2
put t2[i2] into L2
else
-- put L1 & cr after t1only
end if
end repeat
if i2 < limit2 then
repeat with i = i2 to limit2-1
put t2[i] & cr after t2only
end repeat
end if
put "loop" && the millisecs - tStart & cr after msg
end mouseup
Greetings,
Wouter
More information about the use-livecode
mailing list