简体   繁体   English

在Java中使用Casting是否有任何运行时成本?

[英]Is there any runtime cost for Casting in Java?

Would there be any performance differences between these two chunks? 这两个块之间会有任何性能差异吗?

public void doSomething(Supertype input)
{
    Subtype foo = (Subtype)input;
    foo.methodA();
    foo.methodB();
}

vs.

public void doSomething(Supertype input)
{
    ((Subtype)input).methodA();
    ((Subtype)input).methodB();
}

Any other considerations or recommendations between these two? 这两者之间的任何其他考虑或建议?

Well, the compiled code probably includes the cast twice in the second case - so in theory it's doing the same work twice. 好吧,编译后的代码可能在第二种情况下包括两次演员 - 所以从理论上说它做了两次同样的工作。 However, it's very possible that a smart JIT will work out that you're doing the same cast on the same value, so it can cache the result. 但是,智能JIT很可能会计算出您在同一个值上进行相同的转换,因此它可以缓存结果。 But it is having to do work at least once - after all, it needs to make a decision as to whether to allow the cast to succeed, or throw an exception. 不得不做的工作至少一次-毕竟,它需要做出决定是否允许投成功,或抛出异常。

As ever, you should test and profile your code if you care about the performance - but I'd personally use the first form anyway, just because it looks more readable to me. 如果您关心性能,您应该测试和分析您的代码 - 但我个人仍然使用第一个表单,只是因为它看起来更具可读性。

Yes. 是。 Checks must be done with each cast along with the actual mechanism of casting, so casting multiple times will cost more than casting once. 必须对每个铸件进行检查以及实际的铸造机制,因此多次铸造将比铸造一次花费更多。 However, that's the type of thing that the compiler would likely optimize away. 但是,这是编译器可能优化的类型。 It can clearly see that input hasn't changed its type since the last cast and should be able to avoid multiple casts - or at least avoid some of the casting checks. 它可以清楚地看到输入自上次演员后没有改变它的类型,并且应该能够避免多次演员 - 或者至少避免一些演员检查。

In any case, if you're really that worried about efficiency, I'd wonder whether Java is the language that you should be using. 在任何情况下,如果你真的那么担心效率,我想知道的Java是否是你应该使用的语言。

Personally, I'd say to use the first one. 就个人而言,我会说要使用第一个。 Not only is it more readable, but it makes it easier to change the type later. 它不仅更具可读性,而且以后更容易更改类型。 You'll only have to change it in one place instead of every time that you call a function on that variable. 您只需要在一个地方更改它,而不是每次在该变量上调用函数。

I agree with Jon's comment, do it once, but for what it's worth in the general question of "is casting expensive", from what I remember: Java 1.4 improved this noticeably with Java 5 making casts extremely inexpensive. 我同意Jon的评论,做一次,但是从我记忆中的“铸造昂贵”的一般性问题来看它的价值是什么:Java 1.4使用Java 5显着改进了这一点,使得演员阵容非常便宜。 Unless you are writing a game engine, I don't know if it's something to fret about anymore. 除非你正在编写一个游戏引擎,否则我不知道它是否会让人烦恼不已。 I'd worry more about auto-boxing/unboxing and hidden object creation instead. 我更担心自动装箱/拆箱和隐藏物品的创建。

Acording to this article , there is a cost associated with casting. 根据这篇文章 ,铸造会产生相关成本。

Please note that the article is from 1999 and it is up to the reader to decide if the information is still trustworthy! 请注意,该文章是从1999年开始的,由读者决定信息是否仍然值得信赖!

In the first case : 在第一种情况下:

Subtype foo = (Subtype)input;

it is determined at compile time, so no cost at runtime. 它是在编译时确定的,因此在运行时没有成本。

In the second case : 在第二种情况:

((Subtype)input).methodA();

it is determined at run time because compiler will not know. 它是在运行时确定的,因为编译器不会知道。 The jvm has to check if it can converted to a reference of Subtype and if not throw ClassCastException etc. So there will be some cost. jvm必须检查它是否可以转换为Subtype的引用,如果没有抛出ClassCastException等。所以会有一些成本。

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

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