Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package playground;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
@Fork(value = 2) //, jvmArgsAppend = "-XX:TieredStopAtLevel=1")
@Measurement(iterations = 5)
@Warmup(iterations = 5)
public class CapturingLambda {
private Integer param;
@Setup
public void setup() throws Exception {
param = 3;
}
@Benchmark
public Integer returnParam() {
return param;
}
@Benchmark
public Integer returnViaClosure() {
return call(() -> param);
}
@CompilerControl(CompilerControl.Mode.INLINE)
public Integer call(Supplier<Integer> supplier) {
return supplier.get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.