Why are interface method invocations slower than concrete invocations?

后端 未结 6 936
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 15:24

This is question comes in mind when I finding difference between abstract class and interface. In this post I came to know that interfaces are slow as they required extra in

6条回答
  •  情深已故
    2020-11-27 15:57

    There are many performance myths, and some were probably true several years ago, and some might still be true on VMs that don't have a JIT.

    The Android documentation (remember that Android don't have a JVM, they have Dalvik VM) used to say that invoking a method on an interfaces was slower than invoking it on a class, so they were contributing to spreading the myth (it's also possible that it was slower on the Dalvik VM before they turned on the JIT). The documentation does now say:

    Performance Myths

    Previous versions of this document made various misleading claims. We address some of them here.

    On devices without a JIT, it is true that invoking methods via a variable with an exact type rather than an interface is slightly more efficient. (So, for example, it was cheaper to invoke methods on a HashMap map than a Map map, even though in both cases the map was a HashMap.) It was not the case that this was 2x slower; the actual difference was more like 6% slower. Furthermore, the JIT makes the two effectively indistinguishable.

    Source: Designing for performance on Android

    The same thing is probably true for the JIT in the JVM, it would be very odd otherwise.

提交回复
热议问题