Last active
February 16, 2020 20:35
-
-
Save alblue/aa4453a5b1614ee1084570f32b8b5b95 to your computer and use it in GitHub Desktop.
Demonstration of JMH and StringBuilder/StringBuffer performance - original blog at http://alblue.bandlem.com/2016/04/jmh-stringbuffer-stringbuilder.html
This file contains hidden or 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>org.example</groupId> | |
<artifactId>string-benchmark</artifactId> | |
<version>1.0</version> | |
<packaging>jar</packaging> | |
<name>StringBenchmark</name> | |
<prerequisites> | |
<maven>3.0</maven> | |
</prerequisites> | |
<dependencies> | |
<dependency> | |
<groupId>org.openjdk.jmh</groupId> | |
<artifactId>jmh-core</artifactId> | |
<version>${jmh.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.openjdk.jmh</groupId> | |
<artifactId>jmh-generator-annprocess</artifactId> | |
<version>${jmh.version}</version> | |
<scope>provided</scope> | |
</dependency> | |
</dependencies> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<jmh.version>1.12</jmh.version> | |
<javac.target>1.6</javac.target> | |
<uberjar.name>benchmarks</uberjar.name> | |
</properties> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.1</version> | |
<configuration> | |
<compilerVersion>${javac.target}</compilerVersion> | |
<source>${javac.target}</source> | |
<target>${javac.target}</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-shade-plugin</artifactId> | |
<version>2.2</version> | |
<executions> | |
<execution> | |
<phase>package</phase> | |
<goals> | |
<goal>shade</goal> | |
</goals> | |
<configuration> | |
<finalName>${uberjar.name}</finalName> | |
<transformers> | |
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | |
<mainClass>org.openjdk.jmh.Main</mainClass> | |
</transformer> | |
</transformers> | |
<filters> | |
<filter> | |
<artifact>*:*</artifact> | |
<excludes> | |
<exclude>META-INF/*.SF</exclude> | |
<exclude>META-INF/*.DSA</exclude> | |
<exclude>META-INF/*.RSA</exclude> | |
</excludes> | |
</filter> | |
</filters> | |
</configuration> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
<pluginManagement> | |
<plugins> | |
<plugin> | |
<artifactId>maven-clean-plugin</artifactId> | |
<version>2.5</version> | |
</plugin> | |
<plugin> | |
<artifactId>maven-deploy-plugin</artifactId> | |
<version>2.8.1</version> | |
</plugin> | |
<plugin> | |
<artifactId>maven-install-plugin</artifactId> | |
<version>2.5.1</version> | |
</plugin> | |
<plugin> | |
<artifactId>maven-jar-plugin</artifactId> | |
<version>2.4</version> | |
</plugin> | |
<plugin> | |
<artifactId>maven-javadoc-plugin</artifactId> | |
<version>2.9.1</version> | |
</plugin> | |
<plugin> | |
<artifactId>maven-resources-plugin</artifactId> | |
<version>2.6</version> | |
</plugin> | |
<plugin> | |
<artifactId>maven-site-plugin</artifactId> | |
<version>3.3</version> | |
</plugin> | |
<plugin> | |
<artifactId>maven-source-plugin</artifactId> | |
<version>2.2.1</version> | |
</plugin> | |
<plugin> | |
<artifactId>maven-surefire-plugin</artifactId> | |
<version>2.17</version> | |
</plugin> | |
</plugins> | |
</pluginManagement> | |
</build> | |
</project> |
This file contains hidden or 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; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.Param; | |
//import org.openjdk.jmh.annotations.CompilerControl; | |
//import org.openjdk.jmh.annotations.CompilerControl.Mode; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.State; | |
@State(Scope.Benchmark) | |
//@CompilerControl(Mode.EXCLUDE) | |
public class StringBenchmark { | |
private String from = "Alex"; | |
private String to = "Readers"; | |
private String subject = "Benchmarking with JMH"; | |
@Param({"16"}) | |
private int size; | |
@Benchmark | |
public String testEmptyBuffer() { | |
StringBuffer buffer = new StringBuffer(); | |
return buffer.toString(); | |
} | |
@Benchmark | |
public String testEmptyBuilder() { | |
StringBuilder builder = new StringBuilder(); | |
return builder.toString(); | |
} | |
@Benchmark | |
public String testEmptyLiteral() { | |
return ""; | |
} | |
@Benchmark | |
public String testHelloWorldBuilder() { | |
StringBuilder builder = new StringBuilder(); | |
builder.append("Hello"); | |
builder.append("World"); | |
return builder.toString(); | |
} | |
@Benchmark | |
public String testHelloWorldBuffer() { | |
StringBuffer buffer = new StringBuffer(); | |
buffer.append("Hello"); | |
buffer.append("World"); | |
return buffer.toString(); | |
} | |
@Benchmark | |
public String testEmailBuilderSimple() { | |
StringBuilder builder = new StringBuilder(size); | |
builder.append("From"); | |
builder.append(from); | |
builder.append("To"); | |
builder.append(to); | |
builder.append("Subject"); | |
builder.append(subject); | |
return builder.toString(); | |
} | |
@Benchmark | |
public String testEmailBufferSimple() { | |
StringBuffer buffer = new StringBuffer(size); | |
buffer.append("From"); | |
buffer.append(from); | |
buffer.append("To"); | |
buffer.append(to); | |
buffer.append("Subject"); | |
buffer.append(subject); | |
return buffer.toString(); | |
} | |
@Benchmark | |
public String testEmailBuilderConcat() { | |
StringBuilder builder = new StringBuilder(size); | |
builder.append("From" + from); | |
builder.append("To" + to); | |
builder.append("Subject" + subject); | |
return builder.toString(); | |
} | |
@Benchmark | |
public String testEmailBufferConcat() { | |
StringBuffer buffer = new StringBuffer(size); | |
buffer.append("From" + from); | |
buffer.append("To" + to); | |
buffer.append("Subject" + subject); | |
return buffer.toString(); | |
} | |
@Benchmark | |
public String testEmailBuilderChain() { | |
return new StringBuilder(size).append("From").append(from).append("To").append(to).append("Subject") | |
.append(subject).toString(); | |
} | |
@Benchmark | |
public String testEmailBufferChain() { | |
return new StringBuffer(size).append("From").append(from).append("To").append(to).append("Subject") | |
.append(subject).toString(); | |
} | |
@Benchmark | |
public String testEmailLiteral() { | |
return "From" + from + "To" + to + "Subject" + subject; | |
} | |
} |
If you download this gist to test, you'll need to make a directory src/main/java and then move the source file in there in order to run it. The original blog post corresponding to this gist is at http://alblue.bandlem.com/2016/04/jmh-stringbuffer-stringbuilder.html
The code has been moved here: https://github.com/alblue/com.bandlem.jmh.microopts
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JFYI: http://openjdk.java.net/jeps/280