Last active
February 24, 2017 16:34
-
-
Save terrettaz/acdc6b6af44c3bdb3b0a26f49cbca980 to your computer and use it in GitHub Desktop.
Java Benchmarks of lambdas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Benchmarks of Java 8 | |
*/ | |
package benchmarks; | |
import java.util.concurrent.TimeUnit; | |
import java.util.function.Supplier; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.GenerateMicroBenchmark; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.State; | |
@BenchmarkMode(Mode.SampleTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@Fork(value = 1, jvmArgsAppend = { | |
"-server", | |
"-disablesystemassertions", | |
"-Xms16m", | |
"-Xmx16m", | |
"-XX:+PrintGC", | |
"-XX:+PrintGCTimeStamps", | |
"-Xloggc:target/gc.log"}) | |
@State(Scope.Thread) | |
public class LambdaBenchmark { | |
private static final Integer CACHED = Integer.valueOf(0); | |
private static final int LOOP_TIME = 1000000; | |
private int result; | |
@GenerateMicroBenchmark | |
public void side_effect_free__baseline() { | |
call(); | |
} | |
@GenerateMicroBenchmark | |
public void side_effect_free__lambda() { | |
lazy(() -> call()); | |
} | |
@GenerateMicroBenchmark | |
public void side_effect_free__methodReference() { | |
lazy(this::call); | |
} | |
@GenerateMicroBenchmark | |
public void side_effect__baseline() { | |
int a = call(); | |
result = a + 5554478; | |
} | |
@GenerateMicroBenchmark | |
public void side_effect__lambda() { | |
int a = lazy(() -> call()); | |
result = a + 5554478; | |
} | |
@GenerateMicroBenchmark | |
public void side_effect__methodReference() { | |
int a = lazy(this::call); | |
result = a + 5554478; | |
} | |
@GenerateMicroBenchmark | |
public void extensiveLoop__baseline() { | |
int a; | |
for (int i = 0; i < LOOP_TIME; i++) { | |
a = call(); | |
result = a + i; | |
} | |
} | |
@GenerateMicroBenchmark | |
public void extensiveLoop__lambda() { | |
int a; | |
for (int i = 0; i < LOOP_TIME; i++) { | |
a = lazy(() -> call()); | |
result = a + i; | |
} | |
} | |
@GenerateMicroBenchmark | |
public void extensiveLoop__methodReference() { | |
int a; | |
for (int i = 0; i < LOOP_TIME; i++) { | |
a = lazy(this::call); | |
result = a + i; | |
} | |
} | |
private Integer call() { | |
return CACHED; | |
} | |
private int lazy(Supplier<Integer> supp) { | |
return supp.get(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package benchmarks; | |
import java.util.function.Supplier; | |
public class LambdaMemory { | |
static final Object lock = new Object(); | |
static long threshold = 0; | |
final Integer boxedValue = 0; | |
static void caseOne(LambdaMemory lambdaTest) { | |
for (int i = 0; i <= threshold; i++) { | |
lambdaTest.call(); | |
} | |
} | |
static void caseTwo(LambdaMemory lambdaTest) { | |
for (int i = 0; i <= threshold; i++) { | |
lazyCall(() -> lambdaTest.call()); | |
} | |
} | |
static void caseThree(LambdaMemory lambdaTest) { | |
for (int i = 0; i <= threshold; i++) { | |
lazyCall(lambdaTest::call); | |
} | |
} | |
static int lazyCall(Supplier<Integer> function) { | |
return function.get(); | |
} | |
public static void main(String[] args) throws Exception { | |
System.out.print("press to start"); | |
System.in.read(); | |
System.out.println("Starting .."); | |
LambdaMemory lambdaTest = new LambdaMemory(); | |
threshold = Long.parseLong(args[1]); | |
for (int i = 0; i <= threshold; i++) { | |
switch (Integer.parseInt(args[0])) { | |
case 1: | |
caseOne(lambdaTest); | |
break; | |
case 2: | |
caseTwo(lambdaTest); | |
break; | |
case 3: | |
caseThree(lambdaTest); | |
break; | |
} | |
} | |
System.out.println("Ctrl+C to finish .."); | |
synchronized (lock) { | |
lock.wait(); | |
} | |
} | |
public Integer call() { | |
return boxedValue; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>ch.pterrettaz.playground</groupId> | |
<artifactId>benchmarks</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>Auto-generated JMH benchmark</name> | |
<dependencies> | |
<dependency> | |
<groupId>org.openjdk.jmh</groupId> | |
<artifactId>jmh-core</artifactId> | |
<version>0.5.6</version> | |
</dependency> | |
<dependency> | |
<groupId>org.openjdk.jmh</groupId> | |
<artifactId>jmh-generator-annprocess</artifactId> | |
<version>0.5.6</version> | |
<scope>provided</scope> | |
</dependency> | |
</dependencies> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<build> | |
<sourceDirectory>${basedir}</sourceDirectory> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.0</version> | |
<configuration> | |
<compilerVersion>1.8</compilerVersion> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-shade-plugin</artifactId> | |
<version>2.0</version> | |
<executions> | |
<execution> | |
<phase>package</phase> | |
<goals> | |
<goal>shade</goal> | |
</goals> | |
<configuration> | |
<finalName>microbenchmarks</finalName> | |
<transformers> | |
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | |
<mainClass>org.openjdk.jmh.Main</mainClass> | |
</transformer> | |
</transformers> | |
</configuration> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment