Re: Speed comparison: compare:, methodForSelector: &
Re: Speed comparison: compare:, methodForSelector: &
- Subject: Re: Speed comparison: compare:, methodForSelector: &
- From: Glen Low <email@hidden>
- Date: Mon, 23 Feb 2004 21:07:53 +0800
Here is an interesting observation. Sorting 100,000 random integers in
different ways (with std::sort):
NSNumbers sending compare: message: 5.1 seconds
NSNumbers using methodForSelector: 4.9 seconds
NSNumbers using CFNumberCompare(): 1.6 seconds
As plain integers: 0.5 seconds
I would assume that Cocoa just calls CFNumberCompare(), but that
doesn't explain then, why using a function pointer to the
implementation of compare: is 4 times slower.
Placing the values in an NSMutableArray and sending
sortUsingSelector:@selector(compare:) 4.6 seconds
sortUsingFunction:CFNumberCompare 1.8 seconds
Presumably NSArray's sort algorithm is slower than std::sort, but it
seems to have a way to message objects faster than my
methodForSelector: (and I gues NSArray wouldn't even be able to use
this one, as it cannot be sure the stored objects are all of the same
type).
Interesting. I assume when you use methodForSelector, you are querying
once for the IMP and then applying that multiple times? And that you've
used the correct signature of compare: in your IMP and not the variadic
version? And are you using std::sort on a STL container of wrapped
NSNumbers or NSNumber*?
Your best bet in lieu of actual Apple source code is to fire up Shark
(CHUD in Developer Tools somewhere) and profile your app. You'd be
amazed at where the bottlenecks are -- I had thought the CLI Graphviz
tools were sucking up time doing rendering, but the initial top abuser
was lack of prebinding...
Cheers, Glen Low
---
pixelglow software | simply brilliant stuff
www.pixelglow.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.