Reverse a list

Ben Rubinstein benr_mc at cogapp.com
Mon Feb 16 17:25:01 EST 2015


On 16/02/2015 21:15, Peter M. Brigham wrote:
> As I now understand it, the really big difference is between the repeat for
> n = 1 to… form on the one hand, and the repeat for each… and repeat n times
> forms. The latter 2 are not that different, but when the engine has to
> count lines/items every time, it slows things down a very significant
> amount.

But the point is that the difference isn't in the form of the repeat loop, 
it's in the chunking.

  put line x of tMyBigText

is slow (if x is large) because the engine has to count from the start of 
tMyBigText every time this is executed.  If you do it once, it's not very 
significant; if you do it thousands of times it might be.

But the point I'm trying to make is that that's not about the repeat loop, 
it's about the chunking.

	repeat with i=1 to 100
		put not(x) into x
	end repeat

will probably be insignificantly _faster_ than

	repeat for each line t in tListOf100lines
		put not(x) into x
	end repeat

What makes a difference is the code inside the loop.  If the main purpose of 
your loop is to iterate over every chunk of a container, then there's a 
special loop form optimised for that purpose ('repeat for each').  But it's 
not that 'repeat for each' is inherently faster than 'repeat with'.  It's that
	
	repeat for each line t in tListOf100lines

is faster than
	repeat with i = 1 to 100
		put line i of tListOf100lines into t

Ben









More information about the use-livecode mailing list