Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

Performance difference between Java 8 lambdas and anonymous inner classes

Before Java 8, lambda functionality could be achieved by using anonymous inner classes. For example:

interface Lambda {
    void doStuff();
}

// ...

public void doWithCallback(Lambda callback) {
    // ...
    callback.doStuff();
}

// ...

doWithCallback(new Lambda { 
    public void doStuff() { 
        // ... 
    } 
});

In terms of performance, is there a difference between still using this approach and using the new Java 8 lambdas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Oracle has posted a study comparing performance between Lambdas and anonymous classes

See JDK 8: Lambda Performance Study by Sergey Kuksenko, which is 74 slides long.

Summary: slow to warm up but when JIT inlines it worst case just as fast as anonymous class but can be faster.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...