Fastest way to delete non-contiguous lines in list field
Richard Gaskin
ambassador at fourthworld.com
Tue Jul 1 08:42:01 EDT 2003
Jan Schenkel wrote:
>>>> What's the fastest way to delete non-contiguous
>>>> lines in a list field? These
>>>> lines would be listed in the "hilitedLines".
>> ...
> *sniffles* that's the second time in as many days that
> 'repeat for each' has betrayed me. *sobs*
'repeat for each' seems to remain the saviour of each version; it's the sort
that seems to be the difference.
> By the way, the second script won't work correctly if
> the user also selected the last line of the list
> *grin*
Good catch -- fixed below (along with displaying output to avoid such
oversights going forward <g>).
> Out of curiosity, how fast is this 'obvious' script in
> comparison ?
>
> repeat for each line tLine in tOrigList
> add 1 to i
> if i is not among the items of tSelLines
> then put tLine & return after tList
> end repeat
> delete char -1 of tList
It benchmarks as the slowest of the three.
Here's an interesting thing: Test 2 is faster only when the number of
selected lines is a fraction of the total lines. When the number of
selectedlines is greater than about 20% of the total lines, Test 1 (your
original algorithm) is faster.
Given the greater clarity of your Test 1 and more general speed, I would
advocate using that one. Good work.
This exercise carries a side benefit: it shows that "best" is sometimes not
an absolute, depending on the usage context.
Here's all three in a single script:
on mouseUp
put 1000 into n -- iterations
put fld 1 into tOrigList
put the hilitedLines of fld 1 into tSelLines
--
-- Test 1: Jan, 30 June
put the millisecs into t
repeat n
--
put empty into tList1
put tOrigList into tList1
sort items of tSelLines numeric descending
repeat for each item tLine in tSelLines
delete line tLine of tList1
end repeat
--
end repeat
put the millisecs - t into t1
--
-- Test 2: Richard, 30 June
put the millisecs into t
repeat n
--
put empty into tList2
put tOrigList&cr into tList2
repeat for each item tLine in tSelLines
put "ARBITRARY_DELETE_FLAG" into line tLine of tList2
end repeat
replace "ARBITRARY_DELETE_FLAG"&cr with empty in tList2
--
end repeat
delete char -1 of tList2
put the millisecs - t into t2
--
-- Test 3: Jan, 1 July
put the millisecs into t
repeat n
--
put empty into tList3
put 0 into i
repeat for each line tLine in tOrigList
add 1 to i
if i is not among the items of tSelLines
then put tLine & return after tList3
end repeat
delete char -1 of tList3
--
end repeat
put the millisecs - t into t3
--
put t1 && t2 && t3 &cr&&cr& tList1 &cr&cr& tList2 &cr&cr& tList3
end mouseUp
--
Richard Gaskin
Fourth World Media Corporation
Developer of WebMerge 2.2: Publish any database on any site
___________________________________________________________
Ambassador at FourthWorld.com http://www.FourthWorld.com
Tel: 323-225-3717 AIM: FourthWorldInc
More information about the use-livecode
mailing list