• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Most efficient way to iterate through an NSArray
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Most efficient way to iterate through an NSArray


  • Subject: Re: Most efficient way to iterate through an NSArray
  • From: David Avendasora <email@hidden>
  • Date: Mon, 17 Mar 2008 15:12:20 -0400

It's good to know this. I have just found myself using it a lot and wanted to know if I was doing things the right way or not. If you hadn't noticed, in the past, several of my practices were considered less than optimal. :-)

I guess I now have reading to do on Batch Fetching...

Dave


On Mar 17, 2008, at 2:53 PM, Mike Schrag wrote:

Yeah ... +1 here. By far your performance problems are going to be inefficient use of EOF. For instance, if you're traversing relationships of relationships, you will want to make sure you have batch fetched them appropriately. See ERXRecursiveBatchFetching in Wonder.

ms

On Mar 17, 2008, at 2:47 PM, Chuck Hill wrote:

Enumeration should be slightly slower.

But you are wasting your time looking at this to optimize your WO apps. Go find a real problem.

Chuck


On Mar 17, 2008, at 11:34 AM, Timmy wrote:

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


--

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


_______________________________________________
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



_______________________________________________ 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
References: 
 >Most efficient way to iterate through an NSArray (From: David Avendasora <email@hidden>)
 >Re: Most efficient way to iterate through an NSArray (From: Chuck Hill <email@hidden>)
 >Re: Most efficient way to iterate through an NSArray (From: Timmy <email@hidden>)
 >Re: Most efficient way to iterate through an NSArray (From: Chuck Hill <email@hidden>)
 >Re: Most efficient way to iterate through an NSArray (From: Mike Schrag <email@hidden>)

  • Prev by Date: Re: WWDC 08
  • Next by Date: Re: Most efficient way to iterate through an NSArray
  • Previous by thread: Re: Most efficient way to iterate through an NSArray
  • Next by thread: Re: Most efficient way to iterate through an NSArray
  • Index(es):
    • Date
    • Thread