views:

99

answers:

4

I have some high-performance C++ that I need to interface with Objective-C, is there any performance penalty to just dumping this code into a .mm file that contains my Objective-C code vs. leaving this code in a separate .cpp file and extern-ing all the functions I need to call from the .mm file?

A: 

Objective-C compliles down to to assembly, just like C++, I couldn't see how you would take a perf hit.

Pierreten
It's what you can't see that is the problem here
hhafez
Objective-C does have a runtime overhead due to its message passing.
Yann Ramin
Huh? I basically gave the same answer everyone else gave in different wording, don't understand the downvote. The overhead from message passing is negligible to be nearly inexistant.
Pierreten
In general, yes. In the very specific case of a high performance calculation engine, objc_msgSend() (or even a function call) can be devastating to performance. Nor does Obj-C compile down to assembly anything like C++.
bbum
+1  A: 

At first glance it seems there would be no performance hit, but really, unless you have intimate knowledge of what the compiler and runtime is doing internally especially during optimization then you will never know until you try it. So give it a shot and profile it and find out if there is a performance hit.

hhafez
A: 

No penalty, it's the same compiler processing the C++ portion regardless of which file it is in.

I suspect there may even be some gains if you don't have to wrap your C++ code in "extern C" functions.

Andy Dent
+3  A: 

There are a handful of issues here.

(1) if your C++ engine code is running in isolation -- if the Objective-C is acting as the front-end that triggers the underlying engine -- then there is no penalty at all. The C++ bits in ObjC++ compile just like regular C++.

(2) If you are calling into Objective-C from within the calculation engine, then you might have a performance issue on your hands. There is overhead in calling an Objective-C method -- objc_msgSend() isn't free (but close to it) -- but generally not enough to be a problem in comparison to, say, a function call. However, in highly optimized C++, the compiler there may be optimizations that eliminate, even, function call overhead (it gets complex) to a large extent. An Objective-C method call cannot be inlined or optimized away.

(3) If you haven't measured it and discovered a performance problem, don't worry about it...

bbum