Java – LinkedList – performance decreases with the number of different classes in it

The following code measures the time it takes to call the method handle (object o) 100 times from the interface handler (yes, its quality difference analysis):

package test;

import java.util.LinkedList;

public class Test {

    static int i = 0;

    private interface Handler {
        public void handle(Object o);
    }
    private static class SuperHandler implements Handler {
        public void handle(Object o) { i += 1; }
    }
    private static class NoSuperHandler implements Handler {
        public void handle(Object o) { i += 1; }
    }
    private static class LulSuperHandler implements Handler {
        public void handle(Object o) { i += 1; }
    }
    private static class LilSuperHandler implements Handler {
        public void handle(Object o) { i += 1; }
    }
    private static class LolSuperHandler implements Handler {
        public void handle(Object o) { i += 1; }
    }
    private static class LalSuperHandler implements Handler {
        public void handle(Object o) { i += 1; }
    }
    private static class LylSuperHandler implements Handler {
        public void handle(Object o) { i += 1; }
    }
    private static class LzlSuperHandler implements Handler {
        public void handle(Object o) { i += 1; }
    }

    public static void main(String[] args) {
        LinkedList<Handler> ll = new LinkedList<Handler>();
        for(int j = 0; j < 100; j++) {
            if((j % 8) == 0) ll.add(new SuperHandler());
            if((j % 8) == 1) ll.add(new NoSuperHandler());
            if((j % 8) == 2) ll.add(new LulSuperHandler());
            if((j % 8) == 3) ll.add(new LilSuperHandler());
            if((j % 8) == 4) ll.add(new LolSuperHandler());
            if((j % 8) == 5) ll.add(new LalSuperHandler());
            if((j % 8) == 6) ll.add(new LylSuperHandler());
            if((j % 8) == 7) ll.add(new LzlSuperHandler());
        }
        long begin = System.currentTimeMillis();
        for(int j = 0; j < 1000000; j++) for(Handler h: ll) h.handle(null);
        System.out.println("time in ms: " + (System.currentTimeMillis() - begin));
        System.out.println("i: " + i);
    }
}

The fact is that if the LinkedList contains only one kind of handler, such as superhandler, the execution time is less than that. They are different kinds of handlers such as 2 and 3 Every time I add a new handler to the list, the performance degrades

For example, when I only changed this part, I got better performance than above:

for(int j = 0; j < 100; j++) {
    if((j % 2) == 0) ll.add(new SuperHandler());
    if((j % 2) == 1) ll.add(new NoSuperHandler());
}

Are there any special optimization operations here? How much performance will be reduced in Java architecture? My test is wrong because unused handlers are "deleted" or "hidden" by the compiler? (I'm using Linux Ubuntu – Java 1.7 from Oracle)

Solution

Yes Hotspot handles virtual methods very cleverly If there are only some interface implementations, and these implementations are small, it can avoid the complete VTable lookup by checking the correct type and inline code

When you have several different implementations, it will return to the VTable implementation (hotspot is smart enough to undo optimizations that are no longer useful. I'm surprised they all hang together, but obviously they do.)

Note that this is not a question of how many different classes are in the list - there are more classes For more information, see Peter's answer

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>