-
-
Save anonymous/33c3a0f99d9857ccaf019e861cdc46e0 to your computer and use it in GitHub Desktop.
JMH Micro-benchmark for StackOverflow question http://stackoverflow.com/questions/42627108/best-way-to-convert-iterablecharacter-to-string . Build with "mvn clean install", run with "java -jar target.benchmarks.jar"
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 com.stackoverflow.q42627108; | |
import com.google.common.base.Joiner; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Measurement; | |
import org.openjdk.jmh.annotations.Param; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.Warmup; | |
import org.openjdk.jmh.infra.Blackhole; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.StreamSupport; | |
import static org.openjdk.jmh.annotations.Mode.Throughput; | |
public class CharsToString { | |
@State(Scope.Benchmark) | |
public static class Params { | |
@Param | |
public Mode mode; | |
@Param({"10", "100","1000","100000"}) | |
public int size; | |
@Setup | |
public void prepare() { | |
List<Character> chars = new ArrayList<>(size); | |
for (int i = 0; i < size; i++) { | |
chars.add((char) ('a' + (i % 26))); | |
} | |
characters = chars; | |
} | |
Iterable<Character> characters; | |
} | |
@Benchmark | |
@BenchmarkMode(Throughput) | |
@Warmup(batchSize = 2, iterations = 2) | |
@Measurement(batchSize = 2, iterations = 3) | |
public void stringJoin(Params params, Blackhole blackhole) { | |
blackhole.consume(params.mode.join(params.characters)); | |
} | |
public enum Mode { | |
STREAM_APPEND { | |
@Override | |
String join(Iterable<Character> chars) { | |
return StreamSupport.stream(chars.spliterator(), true) | |
.collect( | |
StringBuilder::new, | |
StringBuilder::append, | |
StringBuilder::append | |
) | |
.toString(); | |
} | |
}, STREAM_JOIN { | |
@Override | |
String join(Iterable<Character> chars) { | |
return StreamSupport.stream(chars.spliterator(), true) | |
.map(Object::toString) | |
.collect(Collectors.joining("")); | |
} | |
}, GUAVA_JOIN { | |
@Override | |
String join(Iterable<Character> chars) { | |
return JOINER.join(chars); | |
} | |
}, ITERABLE_FOREACH { | |
@Override | |
String join(Iterable<Character> chars) { | |
StringBuilder sb = new StringBuilder(); | |
chars.forEach(sb::append); | |
return sb.toString(); | |
} | |
}, JAVA7 { | |
@Override | |
String join(Iterable<Character> chars) { | |
StringBuilder sb = new StringBuilder(); | |
for (Character c : chars) { | |
sb.append(c); | |
} | |
return sb.toString(); | |
} | |
}; | |
static final Joiner JOINER = Joiner.on(""); | |
abstract String join(Iterable<Character> chars); | |
} | |
} |
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>com.stackoverflow</groupId> | |
<artifactId>benchmarks-for-question-42627108</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<name>JMH benchmark sample: Java</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-samples</artifactId> | |
<version>${jmh.version}</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.openjdk.jmh</groupId> | |
<artifactId>jmh-generator-annprocess</artifactId> | |
<version>${jmh.version}</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>19.0</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.hamcrest</groupId> | |
<artifactId>hamcrest-library</artifactId> | |
<version>1.3</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<jmh.version>1.17.2</jmh.version> | |
<javac.target>1.8</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> | |
<!-- | |
Shading signed JARs will fail without this. | |
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar | |
--> | |
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment