[Java] instanceof performance

1. Mito

Insert picture description here

2. Background

Because I do flink stream processing, and then use instanceof to judge the type when processing each data inside, because the final performance is not good, I want to see if it is caused by this.

3. Case

3.1 Case 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){
	}
}

Simple class to record time drift:

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

2.2 Case 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. Conclusion

The data value is the output result of the last test on this machine, which is only for comparison. From 结果上看直接使用instanceof的效率还是很高的.

Published 1235 original articles · praised 464 · 1.57 million views

Guess you like

Origin blog.csdn.net/qq_21383435/article/details/105527125