Created
October 14, 2015 09:27
-
-
Save amaembo/54a3ac86691975466ce3 to your computer and use it in GitHub Desktop.
String join benchmark ( http://stackoverflow.com/q/33022822/4856258 )
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
import java.util.concurrent.TimeUnit; | |
import java.util.stream.*; | |
import java.util.*; | |
import org.openjdk.jmh.infra.Blackhole; | |
import org.openjdk.jmh.annotations.*; | |
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) | |
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.MICROSECONDS) | |
@Fork(3) | |
@State(Scope.Benchmark) | |
public class JoinTest { | |
@Param({"1", "2", "10", "100", "1000"}) | |
private int n; | |
List<String> input; | |
@Setup | |
public void setUp() { | |
input = Stream.generate(() -> "foo").limit(n).collect(Collectors.toList()); | |
} | |
@Benchmark | |
public String stringJoin() { | |
return String.join(",", input); | |
} | |
@Benchmark | |
public String sbFirst() { | |
StringBuilder sb = new StringBuilder(); | |
Iterator<String> iterator = input.iterator(); | |
// First time (no delimiter): | |
if (iterator.hasNext()) { | |
sb.append(iterator.next()); | |
} | |
// Other times (with delimiter): | |
while (iterator.hasNext()) { | |
sb.append(","); | |
sb.append(iterator.next()); | |
} | |
return sb.toString(); | |
} | |
@Benchmark | |
public String sbFlag() { | |
StringBuilder sb = new StringBuilder(); | |
boolean firstTime = true; | |
for (String str : input) { | |
if (firstTime) { | |
firstTime = false; | |
} else { | |
sb.append(","); | |
} | |
sb.append(str); | |
} | |
return sb.toString(); | |
} | |
@Benchmark | |
public String sbSetLength() { | |
StringBuilder sb = new StringBuilder(); | |
for (String str : input) { | |
sb.append(str).append(","); | |
} | |
if(sb.length()>0) sb.setLength(sb.length()-1); | |
return sb.toString(); | |
} | |
} |
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
# JMH 1.11 (released 33 days ago) | |
# VM version: JDK 1.8.0_60, VM 25.60-b23 | |
# VM invoker: C:\Program Files\Java\jre1.8.0_60\bin\java.exe | |
... | |
Benchmark (n) Mode Cnt Score Error Units | |
JoinTest.sbFirst 1 avgt 30 0.029 ± 0.001 us/op | |
JoinTest.sbFirst 2 avgt 30 0.052 ± 0.001 us/op | |
JoinTest.sbFirst 10 avgt 30 0.262 ± 0.002 us/op | |
JoinTest.sbFirst 100 avgt 30 2.314 ± 0.011 us/op | |
JoinTest.sbFirst 1000 avgt 30 23.710 ± 0.516 us/op | |
JoinTest.sbFlag 1 avgt 30 0.033 ± 0.001 us/op | |
JoinTest.sbFlag 2 avgt 30 0.053 ± 0.001 us/op | |
JoinTest.sbFlag 10 avgt 30 0.249 ± 0.002 us/op | |
JoinTest.sbFlag 100 avgt 30 2.177 ± 0.015 us/op | |
JoinTest.sbFlag 1000 avgt 30 21.173 ± 0.109 us/op | |
JoinTest.sbSetLength 1 avgt 30 0.046 ± 0.002 us/op | |
JoinTest.sbSetLength 2 avgt 30 0.063 ± 0.001 us/op | |
JoinTest.sbSetLength 10 avgt 30 0.275 ± 0.001 us/op | |
JoinTest.sbSetLength 100 avgt 30 2.484 ± 0.016 us/op | |
JoinTest.sbSetLength 1000 avgt 30 23.260 ± 0.083 us/op | |
JoinTest.stringJoin 1 avgt 30 0.049 ± 0.002 us/op | |
JoinTest.stringJoin 2 avgt 30 0.077 ± 0.001 us/op | |
JoinTest.stringJoin 10 avgt 30 0.314 ± 0.002 us/op | |
JoinTest.stringJoin 100 avgt 30 2.720 ± 0.026 us/op | |
JoinTest.stringJoin 1000 avgt 30 25.546 ± 0.163 us/op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment