抽象类比Java中的接口快吗? [英] Are abstract classes faster than Interfaces in Java?

查看:198
本文介绍了抽象类比Java中的接口快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据本文这里,在抽象类与接口上,抽象类比接口要快一些,这是为什么呢?您能用JVM来解释使用接口与抽象类的机制吗?

According to this article here, on abstract class vs interface, abstract classes a SLIGHTLY faster than interfaces, why is that and can you explain the mechanics of using interface vs abstract classes in terms of JVM ?

4)Java中抽象类和接口之间的第四个区别 是抽象类比接口快一点,因为 介面会先进行搜寻,然后再呼叫中的任何覆写方法 Java.在大多数情况下,这并不是显着差异,但是如果 您正在编写对时间要求严格的应用程序,那么您可能不想 坚不可摧.

4) The fourth difference between abstract class and interface in Java is that abstract class are slightly faster than interface because interface involves a search before calling any overridden method in Java. This is not a significant difference in most of the cases but if you are writing a time critical application then you may not want to leave any stone unturned.

推荐答案

答案取决于实现细节,但是以这种形式,它是不正确的.实际上,作者确实已经尝试通过使用轻微"一词来躲避事实检查,以免您从未观察到这种性能差异.

The answer is depending on implementation details, but in this form, it is not correct. In fact, the author does already try to dodge a fact-check by using the word "slightly" to be open to the possibility that you never observe such a performance difference.

此板所述,其背后的想法这样的声明是,普通类具有子类继承的可覆盖方法的表(也称为"vtable"),该表可以在末尾添加新方法,并为它们覆盖的方法替换表项.因此,第一个解决方案只需要找到表索引即可记住,因此后续调用仅需要在该索引处调用实际接收器类的方法.

As elaborated in this board, the idea behind such statement is, that an ordinary class has a table of overridable methods (also known as "vtable") which is inherited by subclasses, which may add new methods at the end and replace table entries for methods they override. Hence, the first resolution only needs to find the table index, which can be remembered, so subsequent calls only need to invoke the method of the actual receiver class at that index.

由于接口可以由不具有继承关系的不同类实现,因此这些类的实现方法可能位于不同的表索引处.解决此问题的一种方法是,在接口的表到实际的类的表之间进行某种映射.假设这种双重调度会导致这样的假设,即调用接口方法要比普通方法慢.

Since interfaces may be implemented by different classes not having an inheritance relationship, the implementing methods may be at different table indices for these classes. One way to solve this, is to have some kind of mapping from the interface’s table to the actual class’ table. Assuming such kind of double-dispatch leads to the assumption that invoking interface methods was slower than ordinary methods.

但是,像HotSpot JVM这样的JVM不使用这种双重调度.它们像其他任何虚拟方法调用一样,针对实际的接收器类解析接口方法调用.只要接收方是同一类层次结构的一部分,例如,您在Appendable接口上调用一个方法,并且接收方始终是Writer类的子类,则不需要其他步骤.对于大多数所有接口方法调用,此方法都很好.

However, JVMs like the HotSpot JVM do not use such double-dispatch. They resolve interface method invocations against the actual receiver class like any other virtual method invocation. As long as the receiver is part of the same class hierarchy, say, you invoke a method on the Appendable interface and the receiver is always a subclass of the Writer class, no additional steps are needed. For the majority of all interface method invocations, this works fairly well.

在某些情况下,接口方法的调用最终会在不相关的类的不同实现上结束,例如,当Appendable的方法调用有时有时在StringBuilder时而最终在Writer时而结束,但随后,我们有一个无与伦比的方案.这种特定的调用可能比普通的方法调用稍慢一些,但是由于不可能用抽象类构造相同的场景,因此在这里说它比使用抽象类要慢是没有任何意义的.

There are cases where the interface method invocation will end up at different implementations of unrelated classes, say, when an invocation of a method of Appendable sometimes ends up at StringBuilder and other times at Writer, but then, we have an incomparable scenario. This particular invocation may be slightly slower than an ordinary method invocation, but since it is impossible to construct the same scenario with an abstract class, it does not make any sense to say that it was slower than using an abstract class here.

对于与性能相关的代码部分(也称为热点),JVM将执行运行时优化,这将使这种技术差异始终无关紧要.通常,即使是普通虚拟方法调用的一小笔开销也通常会被消除,因为随后的优化在很大程度上依赖于积极内联目标方法代码的能力,以便能够使用调用方的上下文及其已知的周围条件来优化目标方法.被叫方的代码.

For performance relevant code parts, also known as hot spots, the JVM will perform runtime optimizations which render such technical difference irrelevant anyway. Even the tiny overhead of an ordinary virtual method invocation will usually get eliminated, as the subsequent optimizations heavily rely on the ability to aggressively inline the code of the target method, to be able to use the caller’s context and its known surrounding conditions to optimize the callee’s code.

这篇关于抽象类比Java中的接口快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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