C代码运行得更快吗? [英] Does C code run faster?
问题描述
从Objective-C调用C代码是否有任何性能提升?
Is there any performance gain in calling C code from Objective-C?
我读过某些地方,与使用函数调用的其他语言相比,消息传递速度较慢。因此,如果我从Objective-C代码调用C函数,我是否避免了消息传递开销?
I've read somewhere that message passing is slower compared to other languages that use function calling. So if I call a C function from Objective-C code, am I avoiding the messaging overhead?
在优化性能时,是否建议练习编写最关键的函数和C中的程序而不是使用Objective-C对象?
When optimizing for performance, is it recommended practice to code the most critical functions and procedures in C instead of using Objective-C objects?
编辑:
鉴于有关过早优化和代码可读性的答案数量,我我想澄清我没有考虑常规应用程序,但是非常具体的应用程序,例如:
Given the amount of answers warning about premature optimization and code readability, I want to clarify that I was not thinking on regular applications, but very specific ones such as:
- 图形
- 加密或压缩算法。
- 数学
一般来说,功能或程序不需要OO设计,并且打算用参数多次调用。
And in general, functions or procedures that do not need OO design and are intended to be called many times with parameters.
推荐答案
这是基准测试,用于将消息传递与调用C函数进行比较。以下是调用斐波那契函数的不同实现大约14亿次的结果。
This is a benchmark that compares messaging to calling C functions. Here are the results of calling different implementations of the fibonacci function about 1.4 billion times.
Message Passing 23.495 seconds
IMP Calling 16.033 seconds
C Function 9.709 seconds
所以是的,调用时有一些开销Objective C方法。但是,除了在某些情况下,这不会影响您的应用程序的性能。事实上,消息传递仍然比大多数其他操作更有效例如浮点除法。
So yes, there is some overhead when calling an Objective C method. But, except in some situations, this is not what will impact the performance of your app. In fact, messaging is still more efficient than most other operations such as floating-point division.
此外,大多数应用程序花费大部分时间等待用户输入,下载数据等。
In addition, the majority apps spend most time waiting for user input, downloading data, etc.
这篇关于C代码运行得更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!