Last active
July 6, 2016 22:04
-
-
Save voronaam/961147f647fd87dee47150cf402e9c9f to your computer and use it in GitHub Desktop.
JMHing difference between chained and unchained calls
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 org.sample; | |
/** | |
* Results: | |
* Benchmark Mode Cnt Score Error Units | |
* MyBenchmark.testChain avgt 200 0.019 ± 0.001 us/op | |
* MyBenchmark.testNoChain avgt 200 0.019 ± 0.001 us/op | |
*/ | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
import java.util.concurrent.TimeUnit; | |
public class MyBenchmark { | |
@Benchmark | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.MICROSECONDS) | |
public String testChain() { | |
String foo = " "; | |
StringBuffer buf = new StringBuffer(); | |
buf.append("Hello"); | |
buf.append(foo); | |
buf.append("World"); | |
return buf.toString(); | |
} | |
@Benchmark | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.MICROSECONDS) | |
public String testNoChain() { | |
String foo = " "; | |
StringBuffer buf = new StringBuffer(); | |
buf.append("Hello").append(foo).append("World"); | |
return buf.toString(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment