java优化nitpick:它是更快的转换东西,并让它抛出异常,而不是调用instanceof在转换之前检查? [英] java optimization nitpick: is it faster to cast something and let it throw exception than calling instanceof to check before cast?

查看:210
本文介绍了java优化nitpick:它是更快的转换东西,并让它抛出异常,而不是调用instanceof在转换之前检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何人说任何话之前,我只是出于好奇地问这个问题;我不打算根据这个答案做任何过早的优化。

Before anyone says anything I'm asking this out of curiosity only; I'm not planning to do any premature optimization based off of this answer.

我的问题是使用反射和铸造的速度。标准的说法是反思慢。我的问题是什么部分是缓慢的,为什么;特别是在比较某个对象是否是另一个实例的父对象时。

My question is about speed in using reflection and casting. The standard saying is 'reflection is slow'. My question is what part exactly is slow, and why; particularly in comparing if something is a parent of another instance.

我非常有信心,只需将对象的类与另一个类对象进行比较,比较,大概只是直接比较已经存储在对象的状态的单例对象;但是如果一个类是另一个类的父类怎么办?

I'm pretty confident that just comparing the class of an object to another Class object is about as fast as any comparison, presumably just doing direct comparison of singleton objects that are already stored int he Object's state; but what if one class is a parent of the other?

我通常认为 instanceof 作为常规类检查,但今天我想到了它,似乎一些反射必须发生在 instanceof 工作的场景下。我在网上查了几个地方,有人说 instanceof 很慢;大概是由于需要比较父对象的反射?

I usually think of instanceof as being about as fast as regular class checking, but today I thought about it and it seems that some reflection has to happen 'under the scenes' for instanceof to work. I checked online and found a few places where someone said instanceof is slow; presumably due to reflection required to compare the parent of an object?

这导致了下一个问题,关于只是投射。如果我把一个东西作为一个对象,它不是我得到一个 ClassCastException 。但是这不会发生,如果一个对象本身的父对象。基本上我在做一个 instanceof 调用,或逻辑,这种效果,当我在运行时我不是吗?我从来没有听说过任何提示,投射一个对象可能会很慢。不可否认,并非所有的转换都是提供对象的父对象,但大量的转换给父类。但是从来没有人暗示过这可能很慢。

This lead to the next question, what about just casting. If I cast something as an object it's not I get a ClassCastException. But this doesn't happen if a cast an object to a parent of itself. Essentially I'm doing an instanceof call, or logic to that effect, when I do the cast at run time am I not? I've never heard anyone hint that casting an object could be slow before. Admittedly not all casts are to a parent of the provided object, but plenty of casts are to parent classes. Yet never has anyone hinted at all that this could be slow.

那么它是什么。 instanceof 真的不是那么慢吗?是 instanceof 并且转换到父类是慢吗?或者是否有某种原因转换可以更快,然后 instanceof 调用?

So which is it. Is instanceof really not that slow? Are both instanceof and casting to parent class kind of slow? or is there some reason a cast can be done faster then an instanceof call?

推荐答案

一如既往地尝试并在您的特定情况下查看,但非常昂贵。

-Exceptions are expensive, remarkably so.

- 使用代码流的例外几乎总是一个坏主意

-Using exceptions for code flow is almost always a bad idea

编辑:
Ok我很感兴趣,所以我写了一个快速测试系统

Ok I was interested so I wrote a quick test system

public class Test{

    public Test(){
        B b=new B();
        C c=new C();


        for(int i=0;i<10000;i++){
            testUsingInstanceOf(b);
            testUsingInstanceOf(c);
            testUsingException(b);
            testUsingException(c);
        }
    }

    public static void main(String[] args){

        Test test=new Test();

    }

    public static boolean testUsingInstanceOf(A possiblyB){
        if (possiblyB instanceof B){
            return true;
        }else{
            return false;
        }
    }
    public static boolean testUsingException(A possiblyB){
        try{
            B b=(B)possiblyB;
            return true;
        }catch(Exception e){
            return false;
        }
    }


    private class A{

    }
    private class B extends A{

    }
    private class C extends A{

    }        
}

个人档案结果:

by InstanceOf: 4.43 ms
by Exception: 79.4 ms



如我所说,非常昂贵。

as I say, remarkably expensive

当它总是一个B(模拟当你99%确定它的B你只需要确保它仍然没有更快:

And even when its always going to be a B (simulating when you're 99% sure its B you just need to be sure its still no faster:

配置文件结果总是B :

Profile results when always B:

by InstanceOf: 4.48 ms
by Exception: 4.51 ms

这篇关于java优化nitpick:它是更快的转换东西,并让它抛出异常,而不是调用instanceof在转换之前检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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