Ive been wondering, how much faster does c run than objective c? From what I understand c does run faster. I recently implemented a maths function in my app (written in standard c) in the hope that it would increase speed but does it really have that much of an effect?
cheers GC
As others have said, the algorithm is more important than the language. Having said that, there is no question that sometimes you have to optimize speed sensitive code. Every Objective-C method call requires more instructions than a plain-old C function call. Allocating objects in tight game loops is generally a bad idea as well and both iOS and Mac OS X calls tend to allocate a lot of objects.
In the old days, even a C++ method call would be too slow in a tight loop and C++ method calls are generally faster than Objective-C ones. On modern machines you don’t see these sorts of problems as much, but they do still exist. Core Audio filters are a good example of code that needs to be written in plain C rather than Objective-C due to speed issues.
What I would recommend is to write your code using Objective-C and then run it to see if it’s too slow. If so, run Instruments and see where you are spending most of your time. Then optimize that code using plain C, C++, or even assembly language (ok, just kidding about assembly language unless you’re really pushing the envelope).
If you find the method call overhead within loops is slowing you down, you can optimize that by using plain C function calls, inline routines, unrolling the loop, or by using IMP pointers to avoid the method lookup overhead.
If you find that you are copying around data too much, you can optimize that by sharing buffers rather than copying them or maybe using [NSData dataWithBytesNoCopy] rather than [NSData dataWithBytes], etc.
Sometimes you can optimize your graphics so they draw faster — remove transparencies where they’re not needed. Don’t use CALayer shadows or blurs. All new Macs and many iOS devices have two or more CPU cores, so maybe you can offload some processing to the second core using threads. The list goes on and on.
So write your app first using Objective-C using reasonable algorithms and then optimize later when you see where the problems are. Don’t do anything too stupid, like looping n^2 times through a large array in a tight loop, and you’ll probably be ok for 90% of your code.