1 more speed question

Mark Smith mark at maseurope.net
Wed May 18 14:05:09 EDT 2005


I put this in the script of a button:

on mouseUp
   repeat 10000
     add 1 to c
     put c & cr after theList
   end repeat


   put the milliseconds into stTime
   repeat for each line L in theList
     get L
   end repeat
   put the milliseconds - stTime into forTime

   put the milliseconds into stTime
   repeat with n = 1 to the number of lines in theList
     get line n of theList
   end repeat
   put the milliseconds - stTime into withTime

   put forTime && withTime
end mouseUp

forTime was 7, with time was 1197.

My understanding of the issue is that in the 'with' construction, the 
lines have to counted from 1 to get the line you want, each time round. 
With a short list, this is not a problem, but when it's got to count 
thousands of lines (increasing by 1 everytime), it can get slow. The 
'for' construction is effectively keeping a running tally of where it's 
got to, so is only having to add 1 each time. Also, the 'get L' above 
is redundant, since L already contains the data of whichever line it's 
got to.

Imagine having to count from 1 to 9000, then 1 to 9001, 1 to 9002 etc. 
as opposed to simply counting from 1 to 10000.

Cheers,

Mark
On 18 May 2005, at 17:00, jbv wrote:

> for year we've been told that
>     repeat for each line j in myVar
> runs significantly faster than
>     repeat for i=1 to number of lines in myVar
>
> that's true, but if you use the following
>     repeat with i=1 to number of lines in myVar
>         get line i of myVar
>
> and then do all your processing in the "it" variable
> (and then "put it in line i of myVar" at the end of the
> repeat loop if necessary), in that case the "repeat with i..."
> statement seems to run as fast as "repeat for each line..."
>
> does that mean that only the fact that each successive line
> is in a variable (and hence has its content easier to access
> than in a huge variable with dozens or hundreds of lines)
> matters, and that it has nothing to do with the repeat
> structure per se ?


More information about the use-livecode mailing list