Cost of message dispatch in Objective-C

前端 未结 2 2086
执念已碎
执念已碎 2020-12-19 13:33

I\'m curious to know about the cost of message dispatch in Objective-C in various situations. Particularly I want to guide my choice of program design so I\'m not tempted to

2条回答
  •  悲&欢浪女
    2020-12-19 13:52

    First, I'd use the C function, fabs() for this. 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.

    There is no difference at runtime between calling a method on an "id" vs a typed pointer - they go through exactly the same mechanism.

    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.

提交回复
热议问题