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

查看:16
本文介绍了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?

推荐答案

一如既往地尝试并在您的特定情况下查看,但是:

As always try it and see in your particular situations, but:

-异常代价高昂,非常重要.

-Exceptions are expensive, remarkably so.

-对代码流使用异常几乎总是一个坏主意

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

好吧我很感兴趣所以我写了一个快速测试系统

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

正如我所说,非常昂贵

即使它总是会是 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天全站免登陆