【java】instanceof 性能

1.美图

【java】instanceof 性能_第1张图片

2.背景

因为做flink流处理,然后里面处理每条数据的时候使用了instanceof来判断类型,因为最后性能不好,想看看是不是这个引起的。

3.案例

3.1 案例1

public class Test{
     
 
	public static void main(String[] args){
     
		Timer timer = new Timer();
		Man man = new Man();
		Son son = new Son();
		int count = 100000000;
 
		timer.reset();
		for(int i = 0; i < count; i++){
     
			doSomeThing(true);
		}
		timer.printDrift();//62毫秒
 
		timer.reset();
		for(int i = 0; i < count; i++){
     
			doSomeThing(man instanceof IPerson);
		}
		timer.printDrift();//94毫秒
 
		timer.reset();
		for(int i = 0; i < count; i++){
     
			doSomeThing(man instanceof Man);
		}
		timer.printDrift();//78毫秒
 
		timer.reset();
		for(int i = 0; i < count; i++){
     
			doSomeThing(son instanceof Man);
		}
		timer.printDrift();//94毫秒
 
		timer.reset();
		for(int i = 0; i < count; i++){
     
			doSomeThing(man.getClass().isAssignableFrom(IPerson.class));
		}
		timer.printDrift();//4453毫秒
 
		timer.reset();
		for(int i = 0; i < count; i++){
     
			doSomeThing(man.getClass().equals(Man.class));
		}
		timer.printDrift();//375毫秒
 
		timer.reset();
		for(int i = 0; i < count; i++){
     
			doSomeThing(son.getClass().isInstance(man));
		}
		timer.printDrift();//4093毫秒
	}
 
	private static void doSomeThing(boolean bool){
     
	}
}

简单的记录时间漂移的类:

public class Timer{
     
	private long start = 0l;
 
	public void reset(){
     
		start = System.currentTimeMillis();
	}
 
	public void printDrift(){
     
		System.out.println(System.currentTimeMillis() - start);
	}
}

2.2 案例2

 	@Test
    public void instanceOfTest() {
     
        long start = System.currentTimeMillis();
        ClassRoom classRoom = new ClassRoom();
        Person person = new Person();
        for (int i = 0; i < 100000000; i++) {
     
            classRoom.setClassIds(i);
            person.setAge(i);
            if(i%2==0){
     
                if (classRoom instanceof Row) {
     
                    continue;
                }
            }else {
     
                if (person instanceof Row) {
     
                    continue;
                }
            }

        }
        long end = System.currentTimeMillis();
        System.out.println("end:\t"+end);
        System.out.println("start:\t"+start);
        System.out.println(end - start);
        System.out.println((end - start) / 100000000);
    }


    @Test
    public void instanceOfTest1() {
     
        long start = System.currentTimeMillis();

        for (int i = 0; i < 100000000; i++) {
     
            ClassRoom classRoom = new ClassRoom();
            Person person = new Person();
            classRoom.setClassIds(i);
            person.setAge(i);
            if(i%2==0){
     
                if (classRoom instanceof Row) {
     
                    continue;
                }
            }else {
     
                if (person instanceof Row) {
     
                    continue;
                }
            }

        }
        long end = System.currentTimeMillis();
        System.out.println("end:\t"+end);
        System.out.println("start:\t"+start);
        System.out.println(end - start);
        System.out.println((end - start) / 100000000);
    }


3.结论

数据值为在本机上一次测试的输出结果,仅作比较参考。从结果上看直接使用instanceof的效率还是很高的

你可能感兴趣的:(后端-语言-java基础)