Repeat loop index

Dar Scott dsc at swcp.com
Mon Jul 14 11:25:00 EDT 2003


On Monday, July 14, 2003, at 09:19 AM, Trevor DeVore wrote:

> What I do is set i to 1 before the loop starts and then at the end of  
> my repeat loop statements I put "add 1 to i".  That way you keep track  
> of what iteration you are on but you get the speed of the repeat for  
> each.  At least that is my theory.  I haven't done time comparisons.

You are right; "add 1 to i" is constant time for each loop cycle, and  
so will cost you time proportionally to the number of lines.  The  
"repeat with i" _with_ line chunking will cost you proportionally to  
the square of the size of the data.  The "repeat for each" will cost  
you proportional to the size of the data.  ("Size of data" is my fuzzy  
way of vaguely accounting for chars and lines.)

Not only that, that constant time for each loop cycle is very small for  
'add 1 to i".  It takes 0.7 microseconds on my computer.  (BTW, 'put x  
+ 1 into x' takes 1.4 microseconds, so, is fast too, so use whatever is  
readable.)

Dar Scott

************************************************************************ 
****
   Dar Scott Consulting    http://www.swcp.com/dsc/    Programming  
Services
************************************************************************ 
****




More information about the use-livecode mailing list