简体   繁体   English

Objective-C中的消息发送成本

[英]Cost of message dispatch in Objective-C

I'm curious to know about the cost of message dispatch in Objective-C in various situations. 我很想知道在各种情况下Objective-C中的消息发送成本。 Particularly I want to guide my choice of program design so I'm not tempted to prematurely optimize by avoiding message dispatches when they would make for a better design. 特别是我想指导我选择的程序设计,所以我不想通过避免消息发送来过早优化,因为它们可以实现更好的设计。

A case in my current project is that I have a class with instance variables: offsetX and offsetY. 我当前项目中的一个案例是我有一个带有实例变量的类:offsetX和offsetY。 I often want the absolute offset and at the moment I have this line of code all over the place:- 我经常想要绝对偏移量,目前我在这个地方都有这行代码: -

int absOffset = ((offsetX < 0.0) ? -offsetX : offsetX) + 
                 ((offsetY < 0.0) ? -offsetY : offsetY);

Now if this was C++ I would create an inline function that returned the value for absOffset. 现在,如果这是C ++,我将创建一个返回absOffset值的内联函数。 Even in Java/C# I could define such a function as final/sealed and be pretty sure it would be inlined. 即使在Java / C#中,我也可以将这样的函数定义为final / sealed,并且非常确定它将被内联。

The objective-C would be:- 目标-C将是: -

-(int)absOffset {
    return ((offsetX < 0.0) ? -offsetX : offsetX) + 
            ((offsetY < 0.0) ? -offsetY : offsetY);
}

and I would call it like so:- 我会这样称呼它: -

int ao = [self absOffset];

Now, is the compiler able to inline that? 现在,编译器能够内联吗? I assume it is able at least fix it to a direct function call and avoid the dynamic message dispatch that (I assume) objective-c must use because of it's type system. 我假设它至少能够将它修复为直接函数调用并避免动态消息调度(我假设)objective-c必须使用因为它的类型系统。

Also, in general, how much does message dispatch cost in objective-C? 另外,一般来说,客观C中的消息调度成本是多少? Does it differ when calling through an 'id' versus a pointer to a concrete class? 通过'id'调用指向具体类的指针时,它是否有所不同?

Objective C messages are very fast. Objective C消息非常快。 The speed is comparable to C++ virtual method calls, although not quite as fast. 速度与C ++虚拟方法调用相当,但速度不是很快。 Avoiding message passing is definitely premature optimization. 避免消息传递绝对是不成熟的优化。 You might not want to do a lot of it in an inner loop, but the algorithms you choose and other factors will have a much bigger factor on how fast your code is. 您可能不希望在内部循环中执行大量操作,但您选择的算法和其他因素将对代码的速度有更大的影响。 If it is too slow, use a profiler and go from there. 如果它太慢,请使用分析器并从那里开始。

First, I'd use the C function, fabs() for this. 首先,我将使用C函数fabs()。 For other things writing simple, inline, C functions for little helper cases can work well. 对于其他一些简单的内联函数来说,小函数的辅助函数可以很好地工作。 Using methods for convenience rather than discreet behaviour can be a sign of bad design. 使用方法而不是谨慎的行为可能是糟糕设计的标志。 Performance doesn't even come into it yet. 性能甚至还没有进入。

Next, the compiler cannot optimise a method call away. 接下来,编译器无法优化方法调用。 It's a dynamic language, the call is not resolved until runtime. 它是一种动态语言,直到运行时才调用该调用。 Various Objective-C techniques could defeat any attempt of the compiler to do so. 各种Objective-C技术可能会破坏编译器的任何尝试。

There is no difference at runtime between calling a method on an "id" vs a typed pointer - they go through exactly the same mechanism. 调用“id”上的方法与类型指针之间在运行时没有区别 - 它们完全采用相同的机制。

Finally, if you're thinking about the performance characteristics before measuring you are already prematurely optimising. 最后,如果您在测量之前考虑性能特征,那么您已经过早地进行了优化。 That's not to say that is never appropriate, as some might have you believe, but it does usually hold true. 这并不是说这种情况永远不合适,因为有些人可能会相信,但它通常都适用。 In this case, I think, if you put the design first you'll probably end up with a decent enough performance profile anyway. 在这种情况下,我认为,如果你把设计放在首位,你可能最终会得到一个足够好的性能配置文件。 Measure and optimise later, as necessary. 必要时,稍后进行测量和优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM