为什么接口方法调用比具体调用慢? [英] Why are interface method invocations slower than concrete invocations?

查看:430
本文介绍了为什么接口方法调用比具体调用慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发现抽象类和接口之间的区别时,会想到这个问题。
这篇文章中,我开始了解界面他们需要额外的间接,因此很慢。
但是我没有得到接口所需的什么类型的间接,而不是抽象类或具体类。请澄清它。
提前致谢

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 indirection. But I am not getting what type of indirection required by the interface and not by the abstract class or concrete class.Please clarify on it. Thanks in advance

推荐答案

有很多表现神话,有些可能是几年前的事,有些可能在没有JIT的VM上仍然如此。

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.

Android文档(记住Android没有JVM,他们有Dalvik VM)曾经说过在接口上调用方法比调用它要慢一个类,所以他们有助于传播神话(在他们打开JIT之前,它也可能在Dalvik VM上更慢)。文档现在说:

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.

在没有JIT的设备上,通过
变量调用方法确实是一个确切的类型而不是比一个界面更有效
。 (例如,调用
HashMap映射上的方法比使用Map映射更便宜,即使在这两种情况下映射都是
HashMap。)不是2x的情况。慢点;实际的
差异更像是慢了6%。此外,JIT使两个
实际上无法区分。

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.

来源:设计Android上的性能

同样的事情可能是真的对于JVM中的JIT,否则会很奇怪。

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

这篇关于为什么接口方法调用比具体调用慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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