Notifications
Clear all

[Closed] simple for loop / array question

I’ve got a simple question about for loops and arrays. What’s the difference between using

for i = 1 to theArray.count do ( ... )

and

for i in theArray do ( ... )

are they the same? is one faster than the other ? anything else i should be aware of?

6 Replies
1 Reply
(@bobo)
Joined: 10 months ago

Posts: 0

Good question.

Both can be used to do the same, but have different strenghts:

The first version gives you an index, so if you want to drive a progress bar display, it is usually the way to go – you can still get the i-th object, but you also have the number of objects you have processed so far for free.

For example,

for i = 1 to theArray.cound do
 (
 prg_bar.value = 100.0*i/theArray.count --assuming prg_bar is a progressbar control
 ...
 )

while in the second case, you would have to add your own counter to do the same:

cnt = 0
 for i in theArray do
 (
 cnt+=1
 prg_bar.value = 100.0*cnt/theArray.count
 ...
 )

Also, the first form allows you to loop backwards which is essential if you are removing elements from the array while processing it.

When it comes to speed though, the direct loop through the array elements is faster.
I created an array of 10000 elements and looped through it 1000 times to get a significant time value to measure. The inner loop itself didn’t perform any actual work, only looped and accessed the i-th element.

(
 theArray = for i = 1 to 10000 collect random 1 100
 st = timestamp()
 for j = 1 to 1000 do
   for i = 1 to theArray.count do theArray[i]
 format "% ms
" (timestamp()-st)
 )
 

This code executes on my machine running Max 9 32 bit in 5.6 seconds (+/- 0.1 seconds).
Removing the actual access to the array element by index (theArray[i]) reduces this to 4.6 seconds (for example, if you were running from 1 to the array count but NOT using the array elements for anything, which is highly improbable).

In comparison,

(
 theArray = for i = 1 to 10000 collect random 1 100
 st = timestamp()
 for j = 1 to 1000 do
   for i in theArray do i
 format "% ms
" (timestamp()-st)
 )
 

executes on the same machine and Max version in 4.3 seconds (+/- 0.1 sec.) which is 1.3 times faster. Since the variable i already contains the n-th element, having …do i or …do() takes the same time to loop.

Now for the shocker – if you DO need the counter for something else like progress updates, the first approach is almost 4 TIMES faster than the second!

(
 theArray = for i = 1 to 10000 collect random 1 100
 st = timestamp()
 for j = 1 to 1000 do
 (
 cnt = 0
 for i in theArray do cnt +=1
 )
 format "% ms
" (timestamp()-st)
 )

The above code executes in 14.3 seconds because of the memory overhead of overwriting the memory value of the variable cnt.

In other words, if the counter variable is significant for your code, you should use for i = 1 to theArray.count do(), otherwise stick to the direct loop as it is slightly faster.

for i in array do (…)

i will hold the values of what is in the array

for i = 1 to array.count (…)

i holds the index of the count so you could refer to array[i] to get the actual element

for instance
for i in objects do (…) — o actually holds the objects, not the index of an objects array, so you could refer o.material, etc, whereas if you did the i=1 to, you could not access i.material because i is just the integer.

Helps?
Colin

thanks heaps guys. Bobo that was quite an answer! most informative

1 Reply
(@bobo)
Joined: 10 months ago

Posts: 0

This will be included in the FAQ of the next MAXScript Reference – one more reason to look forward to Max 10

Good and great explanation bobo.as always .

So you already working in new max0 documentation

thanks Bobo you just made my day :).
-Baker