• 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: array performance
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: array performance


  • Subject: Re: array performance
  • From: Pete Yandell <email@hidden>
  • Date: Sat, 14 Jun 2003 10:51:09 +1000

Sorting an array of 1,000,000 objects from scratch takes a long time. (But that's a very different thing from "keeping an array sorted" of course...what's your particular application?)

Here are some results I got from doing array performance tests a while back. These are for sorting arrays of 1,000,000 random integers. The times are all CPU usage times on my 400MHz Powerbook G4.

Sort a C array of ints using the qsort function: 1.89 seconds
Sort a C array of NSNumbers using the qsort function: 22.08 seconds

Sort an NSMutableArray of NSNumbers using the sortUsingSelector: method: 49.83 seconds
Sort an NSMutableArray of NSNumbers using the sortUsingFunction: method: 21.09 seconds

I also created a MyInteger class which is a very simple wrapper around int. The best I could do with this was:

Sort a C array of MyIntegers using the qsort function: 4.58 seconds
Sort an NSMutableArray of MyIntegers using the sortUsingFunction: method: 6.0 seconds

So there are a few conclusions:

- That the real computational expense is in the comparisons, so it very much depends on what you're sorting

- That sorting Objective-C objects adds a lot of overhead on top of sorting basic C data types

- That sortUsingFunction: has a lot less overhead than sortUsingSelector: (obviously because you're not doing a method lookup for each comparison)

- That, once you factor out the Objective-C overheads, the sortUsing*: methods are pretty well on par with the efficiency of qsort

- That there is a scary amount of overhead in NSNumber accessor methods


On Saturday, June 14, 2003, at 04:04 AM, Jon Hull wrote:

How good is the built in performance of NSArray. I thought I heard someone on this list say that they kept a list with a million items sorted. How long would this take on average?

If the answer is: not that long, then I may have done some premature optimization.

Thanks,
Jon
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


Pete Yandell
http://pete.yandell.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: array performance
      • From: Michael Tsai <email@hidden>
    • Re: array performance
      • From: Wade Tregaskis <email@hidden>
References: 
 >array performance (From: Jon Hull <email@hidden>)

  • Prev by Date: Re: Dictionary or Array?
  • Next by Date: Re: array performance
  • Previous by thread: array performance
  • Next by thread: Re: array performance
  • Index(es):
    • Date
    • Thread