Skip to content

Instantly share code, notes, and snippets.

@ecki
Created August 10, 2014 21:29
Show Gist options
  • Save ecki/5bcc3a4de669414401b6 to your computer and use it in GitHub Desktop.
Save ecki/5bcc3a4de669414401b6 to your computer and use it in GitHub Desktop.
JMH Test to compare single-shot performance if StringBuffer and +=
C:\ws\github\jmh-stringappend>java -jar target\benchmarks.jar ".*"
# VM invoker: C:\Program Files\Java\jdk1.8.0\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 5 iterations, 4 s each
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: net.eckenfels.jmh.StringConcat.plus
# Parameters: (prefixLen = 0)
# Run progress: 0,00% complete, ETA 00:03:45
# Fork: 1 of 1
# Warmup Iteration 1: 164,549 ±(99.9%) 14,700 ns/op
# Warmup Iteration 2: 149,588 ±(99.9%) 44,876 ns/op
# Warmup Iteration 3: 130,926 ±(99.9%) 19,973 ns/op
# Warmup Iteration 4: 119,538 ±(99.9%) 44,972 ns/op
# Warmup Iteration 5: 134,800 ±(99.9%) 60,267 ns/op
Iteration 1: 119,752 ±(99.9%) 19,700 ns/op
Iteration 2: 124,252 ±(99.9%) 11,672 ns/op
Iteration 3: 126,894 ±(99.9%) 13,018 ns/op
Iteration 4: 115,014 ±(99.9%) 2,616 ns/op
Iteration 5: 120,723 ±(99.9%) 19,876 ns/op
Result: 121,327 ±(99.9%) 17,461 ns/op [Average]
Statistics: (min, avg, max) = (115,014, 121,327, 126,894), stdev = 4,535
Confidence interval (99.9%): [103,866, 138,788]
# VM invoker: C:\Program Files\Java\jdk1.8.0\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 5 iterations, 4 s each
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: net.eckenfels.jmh.StringConcat.plus
# Parameters: (prefixLen = 10)
# Run progress: 11,11% complete, ETA 00:03:47
# Fork: 1 of 1
# Warmup Iteration 1: 184,266 ±(99.9%) 23,634 ns/op
# Warmup Iteration 2: 154,776 ±(99.9%) 48,204 ns/op
# Warmup Iteration 3: 141,308 ±(99.9%) 43,344 ns/op
# Warmup Iteration 4: 128,093 ±(99.9%) 28,260 ns/op
# Warmup Iteration 5: 132,894 ±(99.9%) 36,393 ns/op
Iteration 1: 121,459 ±(99.9%) 5,238 ns/op
Iteration 2: 124,983 ±(99.9%) 11,096 ns/op
Iteration 3: 130,229 ±(99.9%) 18,705 ns/op
Iteration 4: 127,159 ±(99.9%) 11,655 ns/op
Iteration 5: 126,728 ±(99.9%) 15,325 ns/op
Result: 126,112 ±(99.9%) 12,381 ns/op [Average]
Statistics: (min, avg, max) = (121,459, 126,112, 130,229), stdev = 3,215
Confidence interval (99.9%): [113,731, 138,492]
# VM invoker: C:\Program Files\Java\jdk1.8.0\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 5 iterations, 4 s each
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: net.eckenfels.jmh.StringConcat.plus
# Parameters: (prefixLen = 100)
# Run progress: 22,22% complete, ETA 00:03:18
# Fork: 1 of 1
# Warmup Iteration 1: 285,092 ±(99.9%) 84,667 ns/op
# Warmup Iteration 2: 207,351 ±(99.9%) 54,801 ns/op
# Warmup Iteration 3: 178,321 ±(99.9%) 10,493 ns/op
# Warmup Iteration 4: 179,090 ±(99.9%) 46,703 ns/op
# Warmup Iteration 5: 178,816 ±(99.9%) 56,461 ns/op
Iteration 1: 184,734 ±(99.9%) 9,860 ns/op
Iteration 2: 183,802 ±(99.9%) 20,968 ns/op
Iteration 3: 181,271 ±(99.9%) 21,785 ns/op
Iteration 4: 173,929 ±(99.9%) 15,734 ns/op
Iteration 5: 183,722 ±(99.9%) 16,138 ns/op
Result: 181,492 ±(99.9%) 17,012 ns/op [Average]
Statistics: (min, avg, max) = (173,929, 181,492, 184,734), stdev = 4,418
Confidence interval (99.9%): [164,479, 198,504]
# VM invoker: C:\Program Files\Java\jdk1.8.0\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 5 iterations, 4 s each
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: net.eckenfels.jmh.StringConcat.plusEquals
# Parameters: (prefixLen = 0)
# Run progress: 33,33% complete, ETA 00:02:49
# Fork: 1 of 1
# Warmup Iteration 1: 154,893 ±(99.9%) 31,610 ns/op
# Warmup Iteration 2: 139,220 ±(99.9%) 35,042 ns/op
# Warmup Iteration 3: 117,608 ±(99.9%) 26,063 ns/op
# Warmup Iteration 4: 124,964 ±(99.9%) 65,670 ns/op
# Warmup Iteration 5: 119,090 ±(99.9%) 25,250 ns/op
Iteration 1: 122,187 ±(99.9%) 18,366 ns/op
Iteration 2: 118,346 ±(99.9%) 24,518 ns/op
Iteration 3: 120,128 ±(99.9%) 25,989 ns/op
Iteration 4: 120,155 ±(99.9%) 16,541 ns/op
Iteration 5: 121,782 ±(99.9%) 13,450 ns/op
Result: 120,520 ±(99.9%) 5,899 ns/op [Average]
Statistics: (min, avg, max) = (118,346, 120,520, 122,187), stdev = 1,532
Confidence interval (99.9%): [114,621, 126,418]
# VM invoker: C:\Program Files\Java\jdk1.8.0\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 5 iterations, 4 s each
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: net.eckenfels.jmh.StringConcat.plusEquals
# Parameters: (prefixLen = 10)
# Run progress: 44,44% complete, ETA 00:02:21
# Fork: 1 of 1
# Warmup Iteration 1: 165,217 ±(99.9%) 27,735 ns/op
# Warmup Iteration 2: 151,958 ±(99.9%) 24,148 ns/op
# Warmup Iteration 3: 131,490 ±(99.9%) 16,808 ns/op
# Warmup Iteration 4: 134,230 ±(99.9%) 56,588 ns/op
# Warmup Iteration 5: 130,802 ±(99.9%) 31,418 ns/op
Iteration 1: 129,055 ±(99.9%) 5,317 ns/op
Iteration 2: 131,955 ±(99.9%) 8,440 ns/op
Iteration 3: 130,526 ±(99.9%) 17,011 ns/op
Iteration 4: 134,233 ±(99.9%) 10,202 ns/op
Iteration 5: 132,668 ±(99.9%) 33,141 ns/op
Result: 131,687 ±(99.9%) 7,651 ns/op [Average]
Statistics: (min, avg, max) = (129,055, 131,687, 134,233), stdev = 1,987
Confidence interval (99.9%): [124,036, 139,339]
# VM invoker: C:\Program Files\Java\jdk1.8.0\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 5 iterations, 4 s each
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: net.eckenfels.jmh.StringConcat.plusEquals
# Parameters: (prefixLen = 100)
# Run progress: 55,56% complete, ETA 00:01:53
# Fork: 1 of 1
# Warmup Iteration 1: 271,450 ±(99.9%) 40,078 ns/op
# Warmup Iteration 2: 190,853 ±(99.9%) 80,292 ns/op
# Warmup Iteration 3: 175,810 ±(99.9%) 31,427 ns/op
# Warmup Iteration 4: 174,442 ±(99.9%) 29,962 ns/op
# Warmup Iteration 5: 174,031 ±(99.9%) 16,737 ns/op
Iteration 1: 173,504 ±(99.9%) 14,247 ns/op
Iteration 2: 172,657 ±(99.9%) 15,955 ns/op
Iteration 3: 175,349 ±(99.9%) 26,369 ns/op
Iteration 4: 173,619 ±(99.9%) 11,184 ns/op
Iteration 5: 171,789 ±(99.9%) 9,736 ns/op
Result: 173,384 ±(99.9%) 5,096 ns/op [Average]
Statistics: (min, avg, max) = (171,789, 173,384, 175,349), stdev = 1,323
Confidence interval (99.9%): [168,288, 178,479]
# VM invoker: C:\Program Files\Java\jdk1.8.0\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 5 iterations, 4 s each
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: net.eckenfels.jmh.StringConcat.stringBuffer
# Parameters: (prefixLen = 0)
# Run progress: 66,67% complete, ETA 00:01:24
# Fork: 1 of 1
# Warmup Iteration 1: 149,639 ±(99.9%) 12,943 ns/op
# Warmup Iteration 2: 132,855 ±(99.9%) 29,392 ns/op
# Warmup Iteration 3: 113,456 ±(99.9%) 32,804 ns/op
# Warmup Iteration 4: 123,952 ±(99.9%) 37,038 ns/op
# Warmup Iteration 5: 126,300 ±(99.9%) 20,661 ns/op
Iteration 1: 116,770 ±(99.9%) 10,217 ns/op
Iteration 2: 120,401 ±(99.9%) 15,019 ns/op
Iteration 3: 116,059 ±(99.9%) 16,244 ns/op
Iteration 4: 121,816 ±(99.9%) 12,851 ns/op
Iteration 5: 118,121 ±(99.9%) 19,440 ns/op
Result: 118,633 ±(99.9%) 9,356 ns/op [Average]
Statistics: (min, avg, max) = (116,059, 118,633, 121,816), stdev = 2,430
Confidence interval (99.9%): [109,277, 127,989]
# VM invoker: C:\Program Files\Java\jdk1.8.0\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 5 iterations, 4 s each
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: net.eckenfels.jmh.StringConcat.stringBuffer
# Parameters: (prefixLen = 10)
# Run progress: 77,78% complete, ETA 00:00:56
# Fork: 1 of 1
# Warmup Iteration 1: 167,370 ±(99.9%) 30,865 ns/op
# Warmup Iteration 2: 159,008 ±(99.9%) 24,842 ns/op
# Warmup Iteration 3: 136,483 ±(99.9%) 32,160 ns/op
# Warmup Iteration 4: 132,270 ±(99.9%) 40,183 ns/op
# Warmup Iteration 5: 130,185 ±(99.9%) 34,602 ns/op
Iteration 1: 125,577 ±(99.9%) 13,745 ns/op
Iteration 2: 129,020 ±(99.9%) 15,581 ns/op
Iteration 3: 128,232 ±(99.9%) 2,641 ns/op
Iteration 4: 121,336 ±(99.9%) 8,881 ns/op
Iteration 5: 125,143 ±(99.9%) 23,294 ns/op
Result: 125,862 ±(99.9%) 11,661 ns/op [Average]
Statistics: (min, avg, max) = (121,336, 125,862, 129,020), stdev = 3,028
Confidence interval (99.9%): [114,201, 137,522]
# VM invoker: C:\Program Files\Java\jdk1.8.0\jre\bin\java.exe
# VM options: <none>
# Warmup: 5 iterations, 1 s each
# Measurement: 5 iterations, 4 s each
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: net.eckenfels.jmh.StringConcat.stringBuffer
# Parameters: (prefixLen = 100)
# Run progress: 88,89% complete, ETA 00:00:28
# Fork: 1 of 1
# Warmup Iteration 1: 280,186 ±(99.9%) 78,156 ns/op
# Warmup Iteration 2: 188,757 ±(99.9%) 32,597 ns/op
# Warmup Iteration 3: 175,571 ±(99.9%) 54,897 ns/op
# Warmup Iteration 4: 174,425 ±(99.9%) 24,287 ns/op
# Warmup Iteration 5: 176,994 ±(99.9%) 57,552 ns/op
Iteration 1: 174,735 ±(99.9%) 16,032 ns/op
Iteration 2: 173,003 ±(99.9%) 4,665 ns/op
Iteration 3: 173,909 ±(99.9%) 11,884 ns/op
Iteration 4: 172,801 ±(99.9%) 3,177 ns/op
Iteration 5: 174,314 ±(99.9%) 15,934 ns/op
Result: 173,753 ±(99.9%) 3,205 ns/op [Average]
Statistics: (min, avg, max) = (172,801, 173,753, 174,735), stdev = 0,832
Confidence interval (99.9%): [170,547, 176,958]
# Run complete. Total time: 00:04:14
Benchmark (prefixLen) Mode Samples Score Score error Units
n.e.j.StringConcat.plus 0 avgt 5 121,327 17,461 ns/op
n.e.j.StringConcat.plus 10 avgt 5 126,112 12,381 ns/op
n.e.j.StringConcat.plus 100 avgt 5 181,492 17,012 ns/op
n.e.j.StringConcat.plusEquals 0 avgt 5 120,520 5,899 ns/op
n.e.j.StringConcat.plusEquals 10 avgt 5 131,687 7,651 ns/op
n.e.j.StringConcat.plusEquals 100 avgt 5 173,384 5,096 ns/op
n.e.j.StringConcat.stringBuffer 0 avgt 5 118,633 9,356 ns/op
n.e.j.StringConcat.stringBuffer 10 avgt 5 125,862 11,661 ns/op
n.e.j.StringConcat.stringBuffer 100 avgt 5 173,753 3,205 ns/op
package net.eckenfels.jmh;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 4, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@Threads(4)
@State(Scope.Benchmark)
public class StringConcat
{
@Param(value={"0", "10", "100"})
int prefixLen;
String prefixString;
@Setup
/** Dynamically constructs test string. */
public void init() {
char[] cs = new char[prefixLen];
for(int i=0;i<prefixLen;i++)
cs[i]='*';
prefixString = new String(cs);
}
@Benchmark
public String plus() {
return prefixString + ".suffix";
}
@Benchmark
public String plusEquals() {
String string = prefixString;
string += ".suffix";
return string;
}
@Benchmark
public String stringBuffer() {
StringBuffer sb = new StringBuffer(prefixString);
sb = sb.append(".suffix");
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment