Objective-C,.m / .mm 性能差异?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2317868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 23:07:59  来源:igfitidea点击:

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

iphoneobjective-cobjective-c++

提问by Sam

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.

我倾向于在创建新类时默认使用 .mm 扩展名,以便以后需要时可以使用 ObjC++。

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

这样做有什么坏处吗?你更喜欢.m什么时候?是否.m编译为更快的可执行文件(因为 C 通常比 C++ 快)?

回答by Barry Wark

The major disadvantage to using .mmover .mfor "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 Clangfrontend 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.

对“普通”Objective-C使用.mmover的主要缺点.m是,Objective-C++ 的编译时间要长得多。这是因为 C++ 编译器比 C 编译器花费的时间更长。使用 Xcode 3.2 及更高版本,Objective-C 代码可以使用Clang前端工具链来显着加快 Objective-C/C 的编译时间。由于 Clang 尚不支持 Objective-C++/C++,这进一步扩大了两者之间的编译时间差距。

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

更好的策略是.m默认使用。如果您需要在开发后期使用 Objective-C++,将文件重命名为使用.mm扩展名并没有什么坏处。如果您在 XCode 中这样做,项目将自动更新以使用新命名的文件。

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.

当然,一旦您尝试在运行时比较 Objective-C++ 与 Objective-C 的性能,所有标准警告都适用。由于Objective-C++ 是C++ 超集,而Objective-C 是C 超集,因此您正在处理两种不同的语言,每种语言在运行时都有性能权衡。鉴于您完全使用 Objective-X,您可能正在编写用户级应用程序(而不是系统级应用程序),并且 C 和 C++ 之间的性能差异可能完全取决于您编写高效算法的能力每种语言。如果您是 C++ 开发人员,您可能会比 C 编写更好的代码,反之亦然。因此,一如既往,请使用适当的工具来完成这项工作。

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

作为参考,您可能也对这个答案感兴趣:C vs C++ (Objective-C vs Objective-C++) for iPhone

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

2012 年 2 月 17 日更新从 Xcode 4.0(使用 LLVM 3.0)开始,Clang 已经支持 Objective-C++。甚至 C++11 的支持现在也非常强大。

回答by rpetrich

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

如果您只使用 C 特性,.mm 文件应该生成与 .m 非常相似的代码

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

当您需要 C++ 功能时,将文件从 .m 重命名为 .mm 也没有缺点