Skip to content

Instantly share code, notes, and snippets.

@qwwdfsad
Created June 4, 2017 18:27
Show Gist options
  • Save qwwdfsad/d393dd7153058710389364a0ae531851 to your computer and use it in GitHub Desktop.
Save qwwdfsad/d393dd7153058710389364a0ae531851 to your computer and use it in GitHub Desktop.
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