views:

8759

answers:

3
+11  Q: 

Objective-C vs C++

At the risk of this turning info flame bait, my curiosity has gotten the better of me yet again and I have to ask what I feel to be a very compelling question.

Is there any hard data comparing Objective-C to C++, specifically on the iPhone but maybe just on the Mac desktop, for performance of various similar language aspects? I am very familiar with this article comparing C and Objective-C, but this is a larger question of comparing two object oriented languages to each other.

For example, is a C++ vtable lookup really faster than an Obj-C message? How much faster? Threading, polymorphism, sorting, etc. Before I go on a quest to build a project with duplicate object models and various test code, I want to know if anybody has already done this and what the results where. This type of testing and comparison is a project in and of itself and can take a considerable amount of time. Maybe this isn't one project, but two and only the outputs can be compared.

I'm looking for hard data, not evangelism. Like many of you I love and hate both languages for various reasons. Furthermore, if there is someone out there actively pursuing this same thing I'd be interesting in pitching in some code to see the end results, and I'm sure others would help out too. My guess is that they both have strengths and weaknesses, my goal is to find out precisely what they are so that they can be avoided/exploited in real-world scenarios.

+9  A: 

It's very hard to collect "hard data" for this, that's not misguiding.

The biggest problem with doing a feature-to-feature comparison like you suggest, is that the two languages encourage very different coding styles. Objective-C is a dynamic language with duck typing, where typical C++ usage is static. The same object-oriented architecture problem would likely have very different ideal solutions using C++ or Objective-C.

My feeling (as I have programmed much in both languages, mostly on huge projects): To maximize Objective-C performance, it has to be written very close to C. Whereas with C++, it's possible to make much more use of the language without any performance penalty compared to C.

Which one is better? I don't know. For pure performance, C++ will always have the edge. But the OOP style of Objective-C definitely has its merits. I definitely think it is easier to keep a sane architecture with it.

kotlinski
On the iPhone, where I care about performance, I find that I use Objective-C only for accessing the APIs that are in Objective-C. I use C for APIs in C (like OpenGL ES) and for my own code. I prefer to use C for anything I think I might port (say, to a Java-based phone, or to JavaScript). Porting from Objective-C to anything else gives me a stomach ache.
Nosredna
I'm not really looking for 'better', but I understand your point. Still, having some numbers to back it up when I walk around the office screaming "c++ is faster" makes me sound like less of an evangelist and more like a pragmatist.
slf
I understand. I think the best idea to make an impact at the office would be to pick some typical code from your app, that is likely ot take a lot of time. Write one C++ and one Objective-C version. Measure, compare, discuss.
kotlinski
+10  A: 

Mike Ash has some hard numbers for performance of various Objective-C method calls versus C and C++ in his post "Performance Comparisons of Common Operations". Also, this post by Savoy Software is an interesting read when it comes to tuning the performance of an iPhone application by using Objective-C++.

I tend to prefer the clean, descriptive syntax of Objective-C over Objective-C++, and have not found the language itself to be the source of my performance bottlenecks. I even tend to do things that I know sacrifice a little bit of performance if they make my code much more maintainable.

Brad Larson
Excellent point. For the record we do not currently have any performance bottlenecks, or are we blaming things on ObjC, quite the contrary. I guess at the end of the day this question is simply to satisfy my lust for scientific fact.
slf
dig up for the great links
slf
A: 

I don't have hard data for Objective C, but I do have a good place to look for C++.

C++ started as C with Classes according to Bjarne Stroustroup in his reflection on the early years of C++ (http://www2.research.att.com/~bs/hopl2.pdf), so C++ can be thought of (like Objective C) as pushing C to its limits for object orientation.

What are those limits? In the 1994-1997 time frame, a lot of researchers figured out that object-orientation came at a cost due to dynamic binding, e.g. when C++ functions are marked virtual and there may/may not be children classes that override these functions. (In Java and C#, all functions expect ctors are inherently virtual, and there isnt' much you can do about it.) In "A Study of Devirtualization Techniques for a Java Just-In-Time Compiler" from researchers at IBM Research Tokyo, they contrast the techniques used to deal with this, including one from Urz Holzle (umnlaut over 'o') and Gerald Aigner. Urz Holzle, in a separate paper with Karel Driesen, had shown that on average 5.7% of time in C++ programs (and up to ~50%) was spent in calling virtual functions (e.g. vtables + thunks). He later worked with some Smalltalk researachers in what ended up the Java HotSpot VM to solve these problems in OO. Some of these features are being backported to C++ (e.g. 'protected' and Exception handling).

As I mentioned, C++ is static typed where Objective C is duck typed. The performance difference in execution (but not lines of code) probably is a result of this difference.

aalok
Objective-C does not use duck typing. Objects either have a specific type or they are of type id. With duck typing, the runtime assigns a type depending on context. The Objective-C runtime does not assign types.
TechZen