开发者

language speeds c vs objective c

Ive been wondering, how much faster开发者_如何学Go 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.


Any language compiled to native code produces roughly the same instructions for the processor on any given task. The language itself does not imply any speed gains. It is a common misconception that assembly is faster than C; C is faster than C++/Objective-C/What have you.

It all boils down to how you design your algorithm. Of course there are exceptions to every rule.


There should be no discernible difference in performance, especially for the type of code you describe which is essentially going to be C code compiled by the Objective-C compiler.


It does boil down to the algorithm, but in assembly you can do things that C generally can not. This includes organizing certain calls of the program so that pushes and pulls to the stack are not required.

If you need to divide by 2 for example a whole lot for a given task, in assembly you can simply do a register shift (or a couple shifts if it is a big number). C will generally have to make a call, push and pull to the stack and the overhead alone makes it that much slower.

So yes, the above is technically an algorithm difference but the assembly programming allows you to do it. Then you apply the idea above to an array of mathematical functions and the speed difference can be pronounced. To minimize the effect on C though is to obviously make the new assembly code into a library for C. You still will have overhead as compared to straight assembler, but then you also get the best algorithm possible with the greater productivity of the higher level language.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜