eodabash
eodabash

Reputation: 949

Performance considerations when mixing C++ and Objective-C in same file

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?

Upvotes: 2

Views: 494

Answers (4)

bbum
bbum

Reputation: 162712

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...

Upvotes: 4

Andy Dent
Andy Dent

Reputation: 17969

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.

Upvotes: 0

hhafez
hhafez

Reputation: 39750

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.

Upvotes: 0

Pierreten
Pierreten

Reputation: 10147

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

Upvotes: -1

Related Questions