Re: Looping - Enumerators

Date : Thu, 12 Apr 2007 09:34:04 -0700
To : xsi(at)Softimage.COM
From : withanar(at)stanwinston.com
Subject : Re: Looping - Enumerators
>>>
I am curious to know the benefits/dissadvantages over the two looping methods?
Am i
correct in thinking that the Enumerator method is faster?

for ( var e = new Enumerator(this_collection); !e.atEnd(); e.moveNext ){}

for ( var i = 0; i < this_collection.Count; i++ ){}

Cheers

Mike.
>>>


Faster is situation and OS dependant. You can create a simple timer and test the
difference in your context and see if it is significant. The last time I did
such a test was on XSI 3.5 in a linux environment, and the Enumerator loop
smoked the count loop by a significant margin.

The two techniques do different things, and it helps to understand how they
work. I'm not a computer science guru by any stretch, but this is my
understanding. (If my assumptions are wrong, feel free to correct them):

The Enumerator function builds an Enumerator object, a data structure designed
to be a single-use link list, like a beaded necklace that pops off one bead at
a time until there are no more beads. Therefore, your main processor hit comes
from the inital investment of building the Enumerator object. Once the
Enumerator object is built, iterating through its items has very little
processor expense.

The Collection.count technique is taking advantage of the fact that your
Collection is already an assembled data structure. However, you are including
the Collection.count property in each iteration of the loop. We don't know how
efficient the Collection Object is at calculating its count property, and you
are asking it to do this each time the loop runs. Storing the count property
into a variable, then using that in the loop removes this burden like so:

n = Collection.count
for(var i=0; while i<n; i++){
   ...
}

After storing the count into a variable, we're dealing with a test that's more
apples to apples. I've found that the Enumerator is faster than Collections for
larger data sets (1000+ items), but slower for small. But then, a small data
set isn't costing you anything anyway, so I default to the Enumerator. A
performance difference of 0.0007 secs per iteration isn't going to register
much for 100 items, but for 1000 or more, it starts to get noticed.

One last thing to keep in mind, once you build your Enumerator, you cannot add
any more objects to it. It's a remove items only situation. This comes in handy
sometimes such as when you want to grab the Selection from a user input and not
worry about it changing as a result of whatever commands are run in the loop.
Meanwhile, a collection or array can continue to grow in dynamic fashion while
you loop through it, and this power can be very important for some logic
situaions, like transversing a tree of children.


-Brad



---
Unsubscribe? Mail Majordomo(at)Softimage.COM with the following text in body:
unsubscribe xsi


Search the XSI List archives here or use the advanced search form to search across mailing lists. Searching help is available.
This site supposedly brought to you by Benjamin Grosser and the Imaging Technology Group.