Strange results in deletion of lines
Geoff Canyon Rev
gcanyon+rev at gmail.com
Tue May 11 01:31:45 EDT 2010
Just in case anyone doesn't realize how different the options are, I
wrote this code.
on mouseUp
repeat 30000
put any char of "abcdefghijklmnopqrstuvwxyz" && random(100000)
&& "this is a test string for a fixed-record-length processing test" &
cr after X
end repeat
delete char -1 of X
put ticks() into t1
repeat 1
put X into X1
repeat with i = the number of lines of X1 down to 1
if char 1 of line i of X1 is "f" then delete line i of X1
end repeat
end repeat
put ticks() - t1 into t1
put ticks() into t2
repeat 100
put X into X2
filter X2 without "f*"
end repeat
put ticks() - t2 into t2
put ticks() into t3
repeat 100 times
put empty into X3
repeat for each line L in X
if char 1 of L is "f" then next repeat
put L & cr after X3
end repeat
delete char -1 of X3
end repeat
put ticks() - t3 into t3
put X1 into fld 1
put X2 into fld 2
put X3 into fld 3
put t1 && t2 && t3 && (X1 is X2) && (X2 is X3)
end mouseUp
Note that the filter and repeat for each options each run 100 times,
while the repeat with i = the number of lines of X1 down to 1 example
runs just once. Here are the results:
3096 99 118 true true
So the filter and repeat for each options run in about a sixtieth of a
second (remember, they each ran 100 times), while the repeat with i
=... option takes almost a minute.
The crucial lesson here is avoid expressions like line i of..., but
only slightly less important is the fact that repeat for each works
about as fast as filter. If you have a complex operation to perform on
each line, running multiple filter operations to get it done is likely
not as efficient as a simple repeat for each loop. As a simple
example, repeat for each is faster here:
on mouseUp
repeat 30000
put any char of "abcdefghijklmnopqrstuvwxyz" && random(100000) && \
"this is a test string for a fixed-record-length
processing test" && \
any char of "abcdefghijklmnopqrstuvwxyz" & cr after X
end repeat
delete char -1 of X
put ticks() into t2
repeat 100
put X into X2
filter X2 without "f*"
filter X2 without "*g"
end repeat
put ticks() - t2 into t2
put ticks() into t3
repeat 100 times
put empty into X3
repeat for each line L in X
if char 1 of L is "f" or char -1 of L is "g" then next repeat
put L & cr after X3
end repeat
delete char -1 of X3
end repeat
put ticks() - t3 into t3
put X1 into fld 1
put X2 into fld 2
put X3 into fld 3
put t2 && t3 && (X2 is X3)
end mouseUp
More information about the use-livecode
mailing list