java中instanceof操作符的复杂性

发布于 2024-12-12 04:44:57 字数 56 浏览 0 评论 0原文

我想知道在java中使用instanceof运算符的计算成本有多高,并且想知道是否有更好的替代方案

I was wondering how computationally expensive is using instanceof operator in java and wanted to know if there are any better alternatives available

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白鸥掠海 2024-12-19 04:44:57

另一种方法是避免使用 instanceof 并正确设计您的类(在面向对象的意义上)。

由于 instanceof 运算符具有相应的“instanceof" 字节码指令,可能不会有更高效的方法;但这也可能取决于实际的 JVM 如何优化。

The alternative is to avoid using instanceof and design your classes properly (in OO sense).

As the instanceof operator has a corresponding "instanceof" byte code instruction, there probably won't be a more performant approach; but this may also depend on how the actual JVM optimizes.

金橙橙 2024-12-19 04:44:57

instanceof 非常快。然而,这通常是设计考虑不周的症状。

它的性能与(成功的)演员阵容大致相同,因为它做的事情大致相同。事实上,该任务大致相当于“虚拟”方法调用。

关于健全的实现:对于类,只需获取运行时类并查看固定偏移量来检查超类(只要 HotSpot 的继承链不超过 8 个类)。接口有点棘手,但通常会缓存任何特定运行时类的最后两个用例。所以这也很快。

instanceof is pretty damn fast. However, it's generally a symptom of a poorly thought out design.

It'll have around the same performance as a (successful) cast, as it is doing much the same thing. Indeed, the task is roughly equivalent to a "virtual" method call.

On sane implementations: For classes, it's just a matter of getting the runtime class and looking at a fixed offset to check the superclass (so long as you don't have an inheritance chain of more than eight classes for HotSpot). Interfaces are a bit more tricky, but generally have the last two used cases for any particular runtime class cached. So that's also fast.

你是暖光i 2024-12-19 04:44:57

我假设您实际上已经分析了您的代码,并发现您对 instanceof 的使用对性能造成了不小的影响?如果不是,那么您几乎肯定正在解决一个不值得您花时间解决的问题。

如果您所做的只是这样的代码:

if ( something instanceof MyClass ) {
    MyClass mySomething = (MyClass) something;
    //...
} else {
    //exceptional case
}

那么可以先尝试强制转换,并允许 ClassCastException 成为您的“例外情况”:

try {
   MyClass mySomething = (MyClass) something;
} catch (ClassCastException cce) {
    //exceptional case
}

现在,虽然可能还为时过早 优化,重新考虑你的设计还为时过早。过度使用 instanceof 是一种设计味道。一般来说,您使用泛型和多态性的方式应该将使用 instanceof (实际上是强制转换)的次数减少到(几乎)为零。

  1. 如果应根据对象的类型运行不同的代码,请考虑将该代码作为对象的实例方法并使不同的类型符合接口。

  2. 如果您发现自己“知道”某个对象属于某种类型,但您执行了某些步骤,使编译器无法跟踪该事实(例如,您将其放入原始 List ),它可能是泛化的候选者。

I assume you have actually profiled your code and found that your use of instanceof is a non-trivial performance hit? If not, you're almost certainly solving a problem that isn't worth your time to be solving.

If all you're doing is code like this:

if ( something instanceof MyClass ) {
    MyClass mySomething = (MyClass) something;
    //...
} else {
    //exceptional case
}

Then it might be possible to try the cast first, and allow the ClassCastException to be your "exceptional case":

try {
   MyClass mySomething = (MyClass) something;
} catch (ClassCastException cce) {
    //exceptional case
}

Now, while it may be premature optimization, it would not be premature to rethink your design. Overuse of instanceof is a design smell. In general, you should be using generics and polymorphism in ways that reduce the number of times you'd use instanceof (and indeed casting) to (nearly) zero.

  1. If different code should be run depending on the type of the object, consider making that code an instance method of the object and having the different types conform to an interface.

  2. If you find yourself "knowing" that an object is of a certain type but you've done some step that makes the compiler lose track of that fact (e.g. you put it into a raw List), it might be a candidate for generification.

扮仙女 2024-12-19 04:44:57

如果您想检查该对象是否是某个类的实例(但不是扩展实现它),可以将类与==进行比较 会更快:

o.getClass() == YourClass.class

否则,由于 instanceof 关键字是为此特定目的而创建的,我不知道如何做得更好。

If you want to check if the object is an instance of a certain class (but not if it extends or implements it), maybe comparing the classes with == would be faster :

o.getClass() == YourClass.class

Otherwise since the instanceof keyword has been created for this specific purpose I don't see how you could ever do better..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文