Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C vs. C speed

Tags:

c

ios

objective-c

This is probably a naive question here but I'll ask it anyway.

I'm working with Core Audio (C API) on iOS and am mixing C with Objective-C. My class has the .mm extension and everything is working so far.

I've read in different places about Objective-C being slow (without much detail given - and I am not making any declaration that it is). I understand about not calling Objective-C from a Core Audio render callback, etc. and the reasons why.

On the other hand, I need to call in to the class that handles the Core Audio stuff from my GUI in order to make various adjustments at runtime. There would be some walking of arrays, mostly, shifting data around that is used by Core Audio. Would there be any benefit speed-wise from writing my functions in C and storing my variables in, say, vectors rather than NSMutableArrays?

I've only been working with Objective-C/iOS for a few months so I don't have any perspective on this.

like image 922
spring Avatar asked Mar 12 '12 14:03

spring


People also ask

Is Objective-C slower than C++?

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.

Is Objective-C or Swift faster?

With improved performance, you get a better user experience, easier redesign, and maintenance, etc. According to Apple, Swift is 2.6 times faster than Objective-C. The fact may be that Swift was created as a new language in order to be “Swift.”

Is Objective-C faster than Java?

The C version ran about 10 times faster than the same Java version. I would suspect that you will typically see Java running about 5 times slower than the equivalent C on the same platform.

Is Objective-C harder than C++?

You can achieve essentially the same things in either language, but in my opinion the C++ syntax is simpler while some of Objective-C's features make certain tasks (such as GUI design) easier thanks to dynamic dispatch.


2 Answers

Objective-C is slightly slower than straight C function calls because of the lookups involved in its dynamic nature. I'll edit this answer with more detail on how it works later if nobody else adds in the detail.

However, more importantly, you are optimizing prematurely. There's a VERY high chance that the extra overhead of Objective-C will have zero noticeable impact on your application's performance.

Take advantage of Objective-C's strengths to design the best written, most object-oriented application possible. If, and only if, testing shows performance problems, optimize those particular areas of the application.

like image 74
wadesworld Avatar answered Sep 21 '22 07:09

wadesworld


The main performance hit with Objective-C is in the work required to dispatch a method invocation. Objective-C is dynamically bound, which means that the object receiving the message (selector) decides what to do with it at run time. This is implemented with a hash table. The selector is hashed (at compile time I think) and mapped to the method that gets invoked via a hash table, and it takes time to do the look up.

Having said that, the method lookup – which happens in objc_msgSend() is highly optimised. In fact, it is hand crafted in assembler. I've heard it said that the overhead compared to a C function call is about 20 machine instructions. Normally, this is not a big deal, but if you are running through a 100,000 element NSArray, looking up each element with -objectAtIndex: that becomes quite a bit of overhead.

In almost every case, however, the extra flexibility and functionality is worth the cost. This is why wadersworld's answer contains fine advice.

Bill Bumgarner has written an awesome set of articles on objc_msgSend()

like image 42
JeremyP Avatar answered Sep 24 '22 07:09

JeremyP