Objective-C,.m / .mm的性能差异? [英] Objective-C, .m / .mm performance difference?

查看:118
本文介绍了Objective-C,.m / .mm的性能差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

是否有任何不利之处这样做?你什么时候想要 .m .m 编译成更快的可执行文件(因为C通常比C ++快)?

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++)?

推荐答案

使用 .mm 超过 .m 的正常Objective-C的主要缺点是Objective-C ++的编译时间明显更高。这是因为C ++编译器比C编译器花费更长的时间。使用Xcode 3.2及更高版本,Objective-C代码可以使用 Clang 前端工具链来显着加快目标 - C / C编译时间。由于Clang还不支持Objective-C ++ / C ++,这进一步扩大了两者之间编译时间的差距。

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.

更好的策略是使用 .m 默认情况下。如果您需要在开发后期使用Objective-C ++,则重命名文件以使用 .mm 扩展名没有任何害处。如果您是在XCode中,那么项目将自动更新以使用新命名的文件。

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.

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

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.

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

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

更新2012年2月17日从Xcode 4.0(使用LLVM 3.0)开始,Clang支持Objective-C ++。即使是C ++ 11支持也非常强大。

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.

这篇关于Objective-C,.m / .mm的性能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆