Reverse a list

J. Landman Gay jacque at hyperactivesw.com
Mon Feb 16 17:01:53 EST 2015


On 2/16/2015 3:02 PM, Mike Kerner wrote:
> At least
> for the case of your squares of integers, I would expect that there is a
> crossover where it's going to be faster to build the list, first.  I don't
> know if that is at 100, 1000, or some bigger number, but n vs. n^2 is a
> very big difference.

If you are using the value of the counter variable directly, it will 
always be faster than building a list first. Counting is slow because 
the engine has to start at the first token and count the chunks every 
time through the loop:

   repeat with x = 1 to 100
     put line x of tData after tList -- recounts from 1 every iteration
   end repeat

So for that, you'd want "repeat for each" which eliminates the counting 
part. But for something like this:

   repeat with x = 1 to 100
     add x to tVal
   end repeat

there is nothing faster. There is no advantage to creating a 100 item 
list and running through it; the value of x is already calculated and 
ready for use without counting anything. Creating a 100-item list first 
will add unnecessary time to the handler to no advantage.

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com




More information about the use-livecode mailing list