UPDATE
A few comments wondering if my benchmark at the bottom was ruined - after introducing a lot of randomness (so that the JIT could not optimize too many things), I still get similar results, so I tend to think that everything is in order.
In the meantime, I met this presentation with a lambda implementation team. Figures 16 show some performance indicators: inner classes and locks have similar lambda performance / non-convertibility 5 times faster.
And @StuartMarks posted this very interesting link that analyzes lambda performance. The bottom line is that compiling the JIT post, lambdas, and anonymous classes are similarly performed in current JVM Hostpot implementations.
YOUR STANDARD
I also checked your test when you posted it. The problem is that it works in just 20 ms for the first method and 2 ms for the second. Although this ratio is 10: 1, it is in no way representative, because the measurement time is too short.
Then I modified your test to allow more JIT warming up, and get similar results as with jmh (i.e. no difference between anonymous class and lambda).
public class Main { static interface ICallback { void payload(); } static void measureAnonymousClass() { final int arr[] = {0}; ICallback clb = new ICallback() { @Override public void payload() { arr[0]++; } }; clb.payload(); } static void measureLambda() { final int arr[] = {0}; ICallback clb = () -> { arr[0]++; }; clb.payload(); } static void runTimed(String message, Runnable act) { long start = System.nanoTime(); for (int i = 0; i < 10_000_000; i++) { act.run(); } long end = System.nanoTime(); System.out.println(message + ":" + (end - start)); } public static void main(String[] args) { runTimed("as lambdas", Main::measureLambda); runTimed("anonymous class", Main::measureAnonymousClass); runTimed("as lambdas", Main::measureLambda); runTimed("anonymous class", Main::measureAnonymousClass); runTimed("as lambdas", Main::measureLambda); runTimed("anonymous class", Main::measureAnonymousClass); runTimed("as lambdas", Main::measureLambda); runTimed("anonymous class", Main::measureAnonymousClass); } }
Last run takes about 28 seconds for both methods.
JMH MICRO BENCHMARK
I missed the same test with jmh , and the bottom line says that the four methods take as much time as the equivalent:
void baseline() { arr[0]++; }
In other words, JIT includes both an anonymous class and lambda, and they take exactly the same time.
The final result:
Benchmark Mean Mean error Units empty_method 1.104 0.043 nsec/op baseline 2.105 0.038 nsec/op anonymousWithArgs 2.107 0.028 nsec/op anonymousWithoutArgs 2.120 0.044 nsec/op lambdaWithArgs 2.116 0.027 nsec/op lambdaWithoutArgs 2.103 0.017 nsec/op