There seems to be a lot of discussion of the various speed merits to C or C++ as compared to say Java or Python, but I rarely see Objective-C mentioned. Roughly where does it fall in terms of language performance?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
Unlike C++, Objective-C is designed as a clean superset of C. The few Objective-C compiler I've used are better known as C compilers, but also handle Objective-C.
So, it's safe to assume that in the code generation level, C and Objective-C are equivalent.
The first difference appears in the OOP ABI, also called "late method binding". Just like in C++, Objective-C relies in compiler-generated function pointer tables that are traversed at runtime.
Unlike C++, however, the binding method is more 'dynamic', and promotes the use of the
Finally, the most important performance issue is the quality of the libraries used. Since Objective-C is only really popular in the Apple systems, it's reasonable to assume you're using it with Cocoa; which is a fine set of high-level libraries. In most cases, you can leave the heavy lifting to them, so your code either don't have to be so fast, or if you do heavy crunching, then it's likely to be a mostly-static code base, roughly similar to plain C.
TL;DR: it's right there with C and C++ languages where it matters most. If you're not getting good performance, check your algorithms; just as in any serious language.
Short answer: It's compiled into a similar format as C/C++/D. It doesn't use a virtual environment like Java/.Net. And it isn't interpreted like Python/Ruby/Lua. So it is on the faster end of the spectrum.
Objective-C is slower than C/C++. The reason being the runtime of Objective-C which dispatches methods lookups dynamically at runtime the same way as Smalltalk, from which it has taken over this execution model. Dispatches all methods at runtime is called "true message sends" as opposed to function call in C/C++ where the function address is determined at compile time (except for C++ virtual methods). But I can't say how much slower Objective-C is. ASAIK it is only used for application development because of the performance penalty.
The fundamental speed differences between Obj-C and C/C++, as Oliver says below, are due to dynamic method dispatch.
This article profiles this overhead in Obj-C http://it.toolbox.com/blogs/macsploitation/bypassing-objectivecs-message-passing-mechanism-for-speed-24946
It also provides a very nice trick for optimizing your Obj-C code when you determine method dispatch (i.e. objc_msgSend) is the limiting factor -- obtain a pointer to the function once, and use it to call the function many times. It shouldn't help too much as Obj-C runtimes do this optimization automatically.
Note that the true cost of dynamic method dispatch is due to cache misses, because it breaks CPU branch prediction. These are hard to profile and it may be that the code cited above does not measure true cache miss cost.
Some more useful discussion is here: http://www.cocoabuilder.com/archive/cocoa/106535-instance-variable-access.html#106605
Bottom line: the biggest differences between languages are your algorithms. Beyond that, there is a fundamental speed difference between Obj-C, C, and C++, due to dynamic or virtual method dispatch. This second point does not appear to be large. And the article above gives a trick to optimize it, if you can find hot spots via profiling, which may be difficult due to CPU cache misses.