Re: Most efficient way to iterate through an NSArray
Re: Most efficient way to iterate through an NSArray
- Subject: Re: Most efficient way to iterate through an NSArray
- From: Timmy <email@hidden>
- Date: Mon, 17 Mar 2008 11:34:13 -0700
All:
How does java Enumeration compare to this with regard to performance?
I iterate through my EO's almost always using Enumeration - um because
that's how I was "learned."
T
-----
Programmer/Analyst
Graduate School of Education and Information Studies
University of California Los Angeles
On Mar 17, 2008, at 9:52 AM, Chuck Hill wrote:
On Mar 17, 2008, at 3:41 AM, David Avendasora wrote:
Hi all,
I find myself often times needing to iterate through an NSArray of
EOs. I'm not sure if the way I'm doing it is the most effecient or
not. Here's what I normally do:
public void doThisForAllProducts(EOEditingContext ec) {
for (Object aProductObject : products().toArray()) {
Product aProduct = (Product) aProductObject;
aProduct.doThis(ec);
}
}
First off, passing an ec to an eo seems pointless / dangerous,
unless you are returning a local instance of it. Which you aren't.
Second, define "efficient". If you mean in terms of execution
speed, that is probably the fastest. An old fashioned for loop
using objectAtIndex() might be a few milliseconds faster. If you
mean efficient as in "least amount of code", rely on KVC:
products().valueForKey("doThis");
Using KVC will be a bit slower than a raw for loop.
This is assuming that products() is an NSArray of Product EOs.
Is this a good / the best way to iterate through NSArrays in general?
I find myself occasionally nesting this loop a couple deep to drill
down into several toMany relationships. For example (as a keypath):
products.billsOfMaterial.routingSteps.components to get all the
components that are possibly used in a product.
Using KVC on that is not likely to yield the results that you expect.
On a side note, and the reason for calling products().toArray() is
to force EOF to get the real EOs and not just faults. If I just
call products() I'll sometimes get EOs, sometimes faults, seemingly
by EOF whim. Is that a bug or expected behaviour?
I don't (don't ask) use the new for style. But there was a bug in
NSArray where iterator() (or some similar method) did not fire the
faults. I thought this had been fixed, but maybe not until 5.4. Of
course, if you are using Wonder's "not broken" NSArray...
Chuck
--
Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems.
http://www.global-village.net/products/practical_webobjects
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden