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?
-
251986 - Brad Cox and Tom Love create Objective-C, announcing "this language has all the memory safety of C combined with all the blazing speed of Smalltalk." Modern historians suspect the two were dyslexic. (source)– Mason WheelerMar 31, 2012 at 5:08
-
4It falls into the range where performance doesn't matter much. It's the sole supported language for interfacing to Cocoa, so if you want to do that, nothing else works at all. For anything else, I'd consider it a terrible choice, regardless of performance.– Jerry CoffinMar 31, 2012 at 5:20
-
1"performance" isn't a characteristic of a language, but of a language implementation and, more importantly, of the programs written in that language. You can write very fast programs in Objective-C, or you can write very slow ones.– CalebApr 1, 2012 at 7:12
-
It fails in having hideous syntax but it isn't a bad performing language based on its current compilers since the performance is more based on the compiler/vm than the actual language.– RigOct 9, 2013 at 15:37
4 Answers
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 id
superclass everywhere, making it slightly slower than C++ in theory. In practice, this difference is way below measurable.
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.
-
4In fact modern objective-c is about as supersetty as modern C++. In C you're allowed to do arithmetic on arbitrary pointers, you can't in ObjC. Also the performance difference due to method lookup is measurable: the fast path of objc_msgSend is about four times heavier than a member function call (and the slow paths are not as fast as the fast path).– user4051Jun 16, 2013 at 5:14
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.
-
1Found this performance comparison with C: rmarcus.info/?p=488 Looks like Objective-C is nevertheless quite close to C.– OlliPJun 15, 2013 at 20:23
-
Would be nice to compare Objective C with the rest of the pack in Benchmarks' Game Jun 15, 2013 at 21:49
-
I think that performance comparison needs to be taken with some piece of salt as the Objective-C code is to some extend C code and not Objecttive-C message sends. I don't know what the downrating is for. I did Smalltalk development for over a decade and Objective-C is based on Smalltalk in many many ways. I think know what I was talking about.– OlliPJun 17, 2013 at 7:11
-
Not the downvoter; am primarily interested in heavily-numerical algorithms, dynamic dispatch doesn't matter much within those. Jun 17, 2013 at 8:58
Short answer: It's compiled into a similar format as C/C++/D/Go/Rust. It doesn't use a virtual environment like Java/.Net. And it isn't interpreted like Python/Ruby/Lua/JavaScript. So it is on the faster end of the spectrum.
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.