views:

66

answers:

1

I'm testing a 2D OpenGL ES based iPhone game on both the iPhone 3G and the iPhone 4. It runs great on the iPhone 4... and pretty well on the 3G. The game state is updated at 60 Hz... and the rendering is done at 60 fps. Would the following conversion from Objective-C to c improve the performance at all?

// current Objective-C function
- (void) updateSomething {
    // simple update like x_position += 2.0;
}

// plain c function
void updateSomething(){
    // simple update like x_position += 2.0;
}

Assume these functions are called at a rate of 60 Hz. The reason I'm curious is this blog post. The author mentions substantial performance improvements when making a similar conversion.

Cheers!

Edit: after running w/ the CPU Sampler instrument in XCode it looks like about 65% of the function calls are to mach_msg_trap. objc_msgSend apparently takes up a trivial amount of resources.

+1  A: 

The answer is: It depends, but probably not. When you're looking at those numbers, it makes method calls look expensive (they're two times slower!), but if you look at it in the context of an actual program, the real takeaway is that both function calls and method calls are essentially free in most situations.

Making something that is called 60 times a second take 100 nanoseconds less per call is a pointless micro-optimization on a processor as powerful as the original iPhone's — a few millionths of a second improvement each second. In real programs, the hit you take for a method call vs. a function will be dwarfed by the actual contents of the method.

The thing to do is to profile your application and see where the most time is actually being taken up. Use that information to improve the algorithms that are taking the most actual time. That's how you'll get real improvement. In all likelihood, objc_msdSend itself will not be anywhere near the biggest offender.

Chuck