MAXScript FAQ > How To Make It Faster >For Loop By Index Vs. For Loop Through Collection |
I've got a simple question about for loops and arrays.
What is the difference between using
Are they the same? Is one faster than the other ? Anything else I should be aware of?
Both can be used to do the same, but have different strengths and performance characteristics.
The first version gives you an index, so if you want to drive a progress bar display, or use the index to access another array with the same amount of elements, 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.
In the second case, you would have to add your own counter local variable to keep track of the current indexes you needed it:
Also, the first form allows you to loop backwards which is essential if you are removing elements from the array while processing it. The following example creates an array of 1000 random integers and then in the second for loop deletes any elements with a value less than 50:
When it comes to speed though, the direct loop through the array elements is faster. Let's create an array of 10000elements and loop through it 1000 times to get a significant time value to measure. The inner loop itself does not perform any actual work, only loops through the array and accesses the i-th element.
This code executes on a particular machine in 5.6 seconds (+/- 0.1 seconds).
Removing the actual access to the array element by index (theArray[i]) reduces this time 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.
executes on the same machine in 4.3 seconds (+/- 0.1 sec.) which is 1.3 times faster. Since the variable i already contains the n-th element, it does not matter whether we actually use the array element value for anything or not.
Now for the surprise - if you DO need the counter for something else like progress updates, the first approach is almost 4 TIMES faster than the second!
The above code executes in 14.3 seconds on the same machine because of the memory overhead involved in overwriting the value of the variable cnt 10 million times.
In other words, if the counter variable is significant for your code, you should use the form for i = 1 to theArray.count do(), otherwise stick to the direct loop through the collection as it is slightly faster in the general case.