views:

120

answers:

2

Are there standard optimization tricks for Objective-C to make for faster execution along the lines of "inlining" frequent methods as in C++ or the "g++ -fast" tag?

Edit: Does anyone have a short example using SEL and IMP when theMethod has two (or more) integers for input?

+5  A: 

Optimization is best handled by the compiler. Macs use GCC, so the standard optimization GCC flag (-Olevel) should work. In XCode, you can set the optimization level in the project settings. If you're not using GCC, check you compiler documentation for how to enable optimization.

outis
That's right -- off memory it's set to [-O3]
SpecialK
Many thanks all for your suggestions and time! If you're willing, can you give a short example using SEL and IMP when theMethod takes two (or more) integers as its input.
SpecialK
@SpecialK: a short example of *what*? Did you mean to comment on dreamlax's answer?
outis
+2  A: 
dreamlax
As with most performance items, you should run Instruments on your code to see where the bottlenecks are. The items above are great... if you need them. If you don't, it just makes the code hard to read.
nall
@nall: Absolutely. I did some basic benchmarking, and in tight loops like the above, bypassing dispatch resulted in about half the execution time (`theMethod` did some basic maths). If a lot of time is spent in `objc_msgSend` (or whatever the method is called these days), then bypassing dispatch may be an option, otherwise, as you say, it'll be more of an obfuscation than an optimisation.
dreamlax
That's a really interesting optimization, but wouldn't it be easier to just make a C function instead of getting the IMP from a method?
eman
@eman: Sure, if you're able to do so; you may not be able to do so if you're using an object from a closed-source framework.
dreamlax