Most Effecient way to repeat a handler/function - Found word(s) list error in the Text body
Alex Tweedly
alex at tweedly.net
Mon Feb 17 17:40:37 EST 2014
On 17/02/2014 21:50, Peter Haworth wrote:
> Have to admit I'm the same. If it's more convenient codingwise to to
> repeat with x=1 to whatever and I know for sure there won;t be many
> iterations to go through, the speed difference is unnoticeable to the user.
>
> I'm also curious about another aspect of this. Is it universally true that
> "repeat with" is always slower than "repeat for" or only when the loop is
> addressing LC chunks of one sort or another?
>
>
It's basically only when addressing chunks such as line, item or word
(when the engine needs to scan from the stat of the data to find the
specified chunk), and so the time grows order N**2 (where N is a
combination of the number of items/lines/words and the number of
chars/bytes it has to scan over -)
Addressing bytes is constant in time, so addressing each byte by
indexing grows linearly.
Addressing chars has been the same as bytes - but full Unicode may
affect that somehow.
And, btw (to my continuing astonishment), addressing backwards from the
end of the data is even slower
if we have K lines of data, then getting "line K" takes just over half
as long as getting "line -1" (see below).
Why ?
-- Alex.
put "x" into line K of tdata
replace CR with "xxx" & CR in tData
put the millisecs into t1
repeat K times
get line 1 of tData
end repeat
put the millisecs - t1 & CR after msg
put the millisecs into t1
repeat K times
get line K of tData
end repeat
put the millisecs - t1 & CR after msg
put the millisecs into t1
repeat K times
get line -1 of tData
end repeat
put the millisecs - t1 & CR after msg
gives
3
523
934
More information about the use-livecode
mailing list