Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C, .m / .mm performance difference?

I tend to use the .mm extension by default when creating new classes so that I can use ObjC++ later on if I require it.

Is there any disadvantage to doing this? When would you prefer .m? Does .m compile to a faster executable (since C is generally faster than C++)?

like image 997
Sam Avatar asked Feb 23 '10 11:02

Sam


People also ask

Which is faster C or Objective-C?

Calling a C function is about 3-6 times faster than calling an Obj-C method, so how can they be equally fast? They can only be equally fast if you never call methods in Obj-C but that some defies the purpose of using Obj-C.

Why is Objective-C mm?

mm is the extension for the C++ compilation unit expected by the Objective-C compiler, while the . m extension is the extension for the C compilation unit expected by the Objective-C compiler. That's assuming you don't use a compiler flag to override the file extension, as this gentleman has done.

What are .MM files?

An MM file contains a "mind map," which is a visual diagram that consists of text, nodes, branches, shapes, and icons that represent ideas. It may be created by a variety of "mind-mapping" programs, such as FreeMind and Freeplane.

What is objective in C Plus Plus?

Objective-C++ is simply source code that mixes Objective-C classes and C++ classes (two entirely unrelated entities). Your C++ code will work, just as before, and the resulting executable will be linked with the Objective-C runtime, so your Objective-C classes will work as well.


2 Answers

The major disadvantage to using .mm over .m for "normal" Objective-C is that compile times are significantly higher for Objective-C++. This is because the C++ compiler takes longer than the C compiler. With Xcode 3.2 and higher, Objective-C code can use the Clang frontend tool chain to significantly speed up Objective-C/C compiling times. Since Clang does not yet support Objective-C++/C++, this further widens the gap in compiling times between the two.

A better strategy is to use .m by default. If you need to use Objective-C++ later in development, there is no harm in renaming the file to use a .mm extension. If you so from within XCode, the project will be automatically updated to use the newly named file.

Of course all of the standard caveats apply once you try to compare Objective-C++ vs. Objective-C performance at run time. Since Objective-C++ is a C++ superset while Objective-C is a C superset, you are dealing with two different languages each with performance tradeoffs at runtime. Given that you're using Objective-X at all, you are likely writing a user-level application (not a systems level app) and the difference in performance between C and C++ wil likely be completely determined by your abilities to code efficient algorithms in each language. If you're a C++ developer, you'll likely code better than in C and visa versa. So, as always, use the appropriate tool for the job.

For reference, you may also be interested in this answer: C vs C++ (Objective-C vs Objective-C++) for iPhone

UPDATE Feb 17, 2012 As of Xcode 4.0 (with LLVM 3.0), Clang has supported Objective-C++. Even C++11 support is quite strong now.

like image 53
Barry Wark Avatar answered Sep 22 '22 00:09

Barry Wark


If you only use C features, .mm files should generate code that performs very similar to .m

There is also no downside to renaming a file from .m to .mm later when you desire C++ features

like image 35
rpetrich Avatar answered Sep 25 '22 00:09

rpetrich