Skip to content

Instantly share code, notes, and snippets.

@jbosboom
Created March 12, 2015 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbosboom/61c35a22136675847193 to your computer and use it in GitHub Desktop.
Save jbosboom/61c35a22136675847193 to your computer and use it in GitHub Desktop.
package com.jeffreybosboom.stringbuilderbench;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
@State(Scope.Thread)
public class StringBuilderBench {
//promote to non-final fields to inhibit constant folding (see JMHSample_10_ConstantFold.java)
private String really = "really ", long_string = "long string.", re = "re", al = "al", ly = "ly ";
@Benchmark
public String implicit() {
String s = "";
for (int i = 0; i < 10000; i++)
s += really;
s += long_string;
return s;
}
@Benchmark
public String explicit() {
String s = "";
for (int i = 0; i < 10000; i++)
s = new StringBuilder().append(s).append(really).toString();
s = new StringBuilder().append(s).append(long_string).toString();
return s;
}
@Benchmark
public String concat() {
String s = "";
for (int i = 0; i < 10000; i++)
s = s.concat(really);
s = s.concat(long_string);
return s;
}
@Benchmark
public String implicit3() {
String s = "";
for (int i = 0; i < 10000; i++) {
s += re;
s += al;
s += ly;
}
s += long_string;
return s;
}
@Benchmark
public String explicit3() {
String s = "";
for (int i = 0; i < 10000; i++) {
s = new StringBuilder().append(s).append(re).toString();
s = new StringBuilder().append(s).append(al).toString();
s = new StringBuilder().append(s).append(ly).toString();
}
s = new StringBuilder().append(s).append(long_string).toString();
return s;
}
@Benchmark
public String concat3() {
String s = "";
for (int i = 0; i < 10000; i++) {
s = s.concat(re);
s = s.concat(al);
s = s.concat(ly);
}
s = s.concat(long_string);
return s;
}
@Benchmark
public String smart() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10000; i++)
sb.append(really);
sb.append(long_string);
return sb.toString();
}
@Benchmark
public String smart3() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10000; i++) {
sb.append(re);
sb.append(al);
sb.append(ly);
}
sb.append(long_string);
return sb.toString();
}
}
# JMH 1.6.2 (released 7 days ago)
# VM invoker: /data/scratch/jbosboom/jdk1.8.0_40/jre/bin/java
# VM options: <none>
# Warmup: 10 iterations, 5 s each
# Measurement: 30 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.jeffreybosboom.stringbuilderbench.StringBuilderBench.concat
# Run progress: 0.00% complete, ETA 00:26:40
# Fork: 1 of 1
# Warmup Iteration 1: 6.777 ops/s
# Warmup Iteration 2: 6.891 ops/s
# Warmup Iteration 3: 9.250 ops/s
# Warmup Iteration 4: 9.545 ops/s
# Warmup Iteration 5: 10.187 ops/s
# Warmup Iteration 6: 10.078 ops/s
# Warmup Iteration 7: 10.719 ops/s
# Warmup Iteration 8: 10.924 ops/s
# Warmup Iteration 9: 10.998 ops/s
# Warmup Iteration 10: 10.923 ops/s
Iteration 1: 10.087 ops/s
Iteration 2: 8.247 ops/s
Iteration 3: 7.528 ops/s
Iteration 4: 9.999 ops/s
Iteration 5: 10.762 ops/s
Iteration 6: 9.126 ops/s
Iteration 7: 9.458 ops/s
Iteration 8: 10.575 ops/s
Iteration 9: 11.012 ops/s
Iteration 10: 10.551 ops/s
Iteration 11: 10.067 ops/s
Iteration 12: 9.181 ops/s
Iteration 13: 8.410 ops/s
Iteration 14: 10.142 ops/s
Iteration 15: 10.819 ops/s
Iteration 16: 10.989 ops/s
Iteration 17: 10.923 ops/s
Iteration 18: 11.047 ops/s
Iteration 19: 8.234 ops/s
Iteration 20: 7.091 ops/s
Iteration 21: 7.082 ops/s
Iteration 22: 7.812 ops/s
Iteration 23: 10.822 ops/s
Iteration 24: 10.996 ops/s
Iteration 25: 11.007 ops/s
Iteration 26: 11.006 ops/s
Iteration 27: 8.973 ops/s
Iteration 28: 7.117 ops/s
Iteration 29: 10.632 ops/s
Iteration 30: 10.860 ops/s
Result: 9.685 ±(99.9%) 0.924 ops/s [Average]
Statistics: (min, avg, max) = (7.082, 9.685, 11.047), stdev = 1.382
Confidence interval (99.9%): [8.762, 10.609]
# JMH 1.6.2 (released 7 days ago)
# VM invoker: /data/scratch/jbosboom/jdk1.8.0_40/jre/bin/java
# VM options: <none>
# Warmup: 10 iterations, 5 s each
# Measurement: 30 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.jeffreybosboom.stringbuilderbench.StringBuilderBench.concat3
# Run progress: 12.50% complete, ETA 00:23:39
# Fork: 1 of 1
# Warmup Iteration 1: 2.521 ops/s
# Warmup Iteration 2: 2.783 ops/s
# Warmup Iteration 3: 2.326 ops/s
# Warmup Iteration 4: 2.696 ops/s
# Warmup Iteration 5: 2.844 ops/s
# Warmup Iteration 6: 2.751 ops/s
# Warmup Iteration 7: 2.966 ops/s
# Warmup Iteration 8: 2.840 ops/s
# Warmup Iteration 9: 2.569 ops/s
# Warmup Iteration 10: 3.038 ops/s
Iteration 1: 2.876 ops/s
Iteration 2: 2.759 ops/s
Iteration 3: 3.216 ops/s
Iteration 4: 2.779 ops/s
Iteration 5: 2.770 ops/s
Iteration 6: 2.910 ops/s
Iteration 7: 2.732 ops/s
Iteration 8: 2.871 ops/s
Iteration 9: 2.905 ops/s
Iteration 10: 2.987 ops/s
Iteration 11: 2.679 ops/s
Iteration 12: 2.999 ops/s
Iteration 13: 2.995 ops/s
Iteration 14: 3.193 ops/s
Iteration 15: 3.227 ops/s
Iteration 16: 3.261 ops/s
Iteration 17: 3.382 ops/s
Iteration 18: 3.368 ops/s
Iteration 19: 3.441 ops/s
Iteration 20: 3.541 ops/s
Iteration 21: 3.599 ops/s
Iteration 22: 3.304 ops/s
Iteration 23: 3.627 ops/s
Iteration 24: 3.688 ops/s
Iteration 25: 3.703 ops/s
Iteration 26: 3.709 ops/s
Iteration 27: 2.812 ops/s
Iteration 28: 3.395 ops/s
Iteration 29: 3.704 ops/s
Iteration 30: 3.028 ops/s
Result: 3.182 ±(99.9%) 0.228 ops/s [Average]
Statistics: (min, avg, max) = (2.679, 3.182, 3.709), stdev = 0.341
Confidence interval (99.9%): [2.954, 3.410]
# JMH 1.6.2 (released 7 days ago)
# VM invoker: /data/scratch/jbosboom/jdk1.8.0_40/jre/bin/java
# VM options: <none>
# Warmup: 10 iterations, 5 s each
# Measurement: 30 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.jeffreybosboom.stringbuilderbench.StringBuilderBench.explicit
# Run progress: 25.00% complete, ETA 00:20:26
# Fork: 1 of 1
# Warmup Iteration 1: 1.565 ops/s
# Warmup Iteration 2: 7.091 ops/s
# Warmup Iteration 3: 9.830 ops/s
# Warmup Iteration 4: 9.013 ops/s
# Warmup Iteration 5: 7.237 ops/s
# Warmup Iteration 6: 7.186 ops/s
# Warmup Iteration 7: 7.385 ops/s
# Warmup Iteration 8: 8.223 ops/s
# Warmup Iteration 9: 10.196 ops/s
# Warmup Iteration 10: 9.749 ops/s
Iteration 1: 9.911 ops/s
Iteration 2: 10.574 ops/s
Iteration 3: 10.835 ops/s
Iteration 4: 9.047 ops/s
Iteration 5: 10.138 ops/s
Iteration 6: 9.845 ops/s
Iteration 7: 10.792 ops/s
Iteration 8: 7.401 ops/s
Iteration 9: 7.199 ops/s
Iteration 10: 7.302 ops/s
Iteration 11: 7.484 ops/s
Iteration 12: 7.417 ops/s
Iteration 13: 9.803 ops/s
Iteration 14: 10.305 ops/s
Iteration 15: 10.298 ops/s
Iteration 16: 9.691 ops/s
Iteration 17: 10.377 ops/s
Iteration 18: 10.701 ops/s
Iteration 19: 7.581 ops/s
Iteration 20: 7.364 ops/s
Iteration 21: 7.437 ops/s
Iteration 22: 7.989 ops/s
Iteration 23: 7.520 ops/s
Iteration 24: 8.628 ops/s
Iteration 25: 7.974 ops/s
Iteration 26: 8.774 ops/s
Iteration 27: 10.551 ops/s
Iteration 28: 10.238 ops/s
Iteration 29: 9.481 ops/s
Iteration 30: 9.690 ops/s
Result: 9.078 ±(99.9%) 0.884 ops/s [Average]
Statistics: (min, avg, max) = (7.199, 9.078, 10.835), stdev = 1.323
Confidence interval (99.9%): [8.194, 9.962]
# JMH 1.6.2 (released 7 days ago)
# VM invoker: /data/scratch/jbosboom/jdk1.8.0_40/jre/bin/java
# VM options: <none>
# Warmup: 10 iterations, 5 s each
# Measurement: 30 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.jeffreybosboom.stringbuilderbench.StringBuilderBench.explicit3
# Run progress: 37.50% complete, ETA 00:16:59
# Fork: 1 of 1
# Warmup Iteration 1: 0.511 ops/s
# Warmup Iteration 2: 0.444 ops/s
# Warmup Iteration 3: 0.488 ops/s
# Warmup Iteration 4: 1.584 ops/s
# Warmup Iteration 5: 9.570 ops/s
# Warmup Iteration 6: 10.081 ops/s
# Warmup Iteration 7: 7.962 ops/s
# Warmup Iteration 8: 7.509 ops/s
# Warmup Iteration 9: 7.103 ops/s
# Warmup Iteration 10: 9.523 ops/s
Iteration 1: 8.569 ops/s
Iteration 2: 8.554 ops/s
Iteration 3: 8.963 ops/s
Iteration 4: 8.357 ops/s
Iteration 5: 8.973 ops/s
Iteration 6: 8.390 ops/s
Iteration 7: 8.315 ops/s
Iteration 8: 8.463 ops/s
Iteration 9: 8.365 ops/s
Iteration 10: 8.607 ops/s
Iteration 11: 8.649 ops/s
Iteration 12: 8.450 ops/s
Iteration 13: 8.996 ops/s
Iteration 14: 8.041 ops/s
Iteration 15: 8.417 ops/s
Iteration 16: 9.429 ops/s
Iteration 17: 8.247 ops/s
Iteration 18: 8.268 ops/s
Iteration 19: 8.269 ops/s
Iteration 20: 8.805 ops/s
Iteration 21: 8.549 ops/s
Iteration 22: 8.700 ops/s
Iteration 23: 8.146 ops/s
Iteration 24: 8.286 ops/s
Iteration 25: 8.987 ops/s
Iteration 26: 8.728 ops/s
Iteration 27: 8.129 ops/s
Iteration 28: 8.980 ops/s
Iteration 29: 8.952 ops/s
Iteration 30: 9.327 ops/s
Result: 8.597 ±(99.9%) 0.237 ops/s [Average]
Statistics: (min, avg, max) = (8.041, 8.597, 9.429), stdev = 0.354
Confidence interval (99.9%): [8.360, 8.834]
# JMH 1.6.2 (released 7 days ago)
# VM invoker: /data/scratch/jbosboom/jdk1.8.0_40/jre/bin/java
# VM options: <none>
# Warmup: 10 iterations, 5 s each
# Measurement: 30 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.jeffreybosboom.stringbuilderbench.StringBuilderBench.implicit
# Run progress: 50.00% complete, ETA 00:13:38
# Fork: 1 of 1
# Warmup Iteration 1: 1.534 ops/s
# Warmup Iteration 2: 6.136 ops/s
# Warmup Iteration 3: 10.235 ops/s
# Warmup Iteration 4: 10.273 ops/s
# Warmup Iteration 5: 10.676 ops/s
# Warmup Iteration 6: 10.948 ops/s
# Warmup Iteration 7: 8.115 ops/s
# Warmup Iteration 8: 10.813 ops/s
# Warmup Iteration 9: 7.350 ops/s
# Warmup Iteration 10: 9.554 ops/s
Iteration 1: 10.275 ops/s
Iteration 2: 10.795 ops/s
Iteration 3: 9.877 ops/s
Iteration 4: 11.061 ops/s
Iteration 5: 11.163 ops/s
Iteration 6: 11.125 ops/s
Iteration 7: 11.140 ops/s
Iteration 8: 11.100 ops/s
Iteration 9: 9.376 ops/s
Iteration 10: 11.165 ops/s
Iteration 11: 11.045 ops/s
Iteration 12: 11.119 ops/s
Iteration 13: 11.152 ops/s
Iteration 14: 8.873 ops/s
Iteration 15: 10.660 ops/s
Iteration 16: 7.297 ops/s
Iteration 17: 7.212 ops/s
Iteration 18: 9.367 ops/s
Iteration 19: 11.126 ops/s
Iteration 20: 11.140 ops/s
Iteration 21: 8.634 ops/s
Iteration 22: 7.160 ops/s
Iteration 23: 10.779 ops/s
Iteration 24: 11.227 ops/s
Iteration 25: 8.859 ops/s
Iteration 26: 11.212 ops/s
Iteration 27: 11.168 ops/s
Iteration 28: 11.258 ops/s
Iteration 29: 11.139 ops/s
Iteration 30: 11.209 ops/s
Result: 10.290 ±(99.9%) 0.878 ops/s [Average]
Statistics: (min, avg, max) = (7.160, 10.290, 11.258), stdev = 1.315
Confidence interval (99.9%): [9.412, 11.169]
# JMH 1.6.2 (released 7 days ago)
# VM invoker: /data/scratch/jbosboom/jdk1.8.0_40/jre/bin/java
# VM options: <none>
# Warmup: 10 iterations, 5 s each
# Measurement: 30 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.jeffreybosboom.stringbuilderbench.StringBuilderBench.implicit3
# Run progress: 62.50% complete, ETA 00:10:12
# Fork: 1 of 1
# Warmup Iteration 1: 0.527 ops/s
# Warmup Iteration 2: 0.642 ops/s
# Warmup Iteration 3: 0.417 ops/s
# Warmup Iteration 4: 4.773 ops/s
# Warmup Iteration 5: 9.694 ops/s
# Warmup Iteration 6: 10.227 ops/s
# Warmup Iteration 7: 9.956 ops/s
# Warmup Iteration 8: 9.660 ops/s
# Warmup Iteration 9: 7.547 ops/s
# Warmup Iteration 10: 7.467 ops/s
Iteration 1: 9.436 ops/s
Iteration 2: 10.723 ops/s
Iteration 3: 8.949 ops/s
Iteration 4: 10.638 ops/s
Iteration 5: 10.904 ops/s
Iteration 6: 10.149 ops/s
Iteration 7: 7.930 ops/s
Iteration 8: 8.354 ops/s
Iteration 9: 11.007 ops/s
Iteration 10: 10.167 ops/s
Iteration 11: 11.039 ops/s
Iteration 12: 10.451 ops/s
Iteration 13: 10.574 ops/s
Iteration 14: 7.500 ops/s
Iteration 15: 7.394 ops/s
Iteration 16: 7.475 ops/s
Iteration 17: 7.572 ops/s
Iteration 18: 10.654 ops/s
Iteration 19: 8.949 ops/s
Iteration 20: 10.344 ops/s
Iteration 21: 8.347 ops/s
Iteration 22: 10.357 ops/s
Iteration 23: 7.527 ops/s
Iteration 24: 7.666 ops/s
Iteration 25: 8.393 ops/s
Iteration 26: 8.633 ops/s
Iteration 27: 9.802 ops/s
Iteration 28: 9.783 ops/s
Iteration 29: 8.993 ops/s
Iteration 30: 9.396 ops/s
Result: 9.303 ±(99.9%) 0.838 ops/s [Average]
Statistics: (min, avg, max) = (7.394, 9.303, 11.039), stdev = 1.254
Confidence interval (99.9%): [8.466, 10.141]
# JMH 1.6.2 (released 7 days ago)
# VM invoker: /data/scratch/jbosboom/jdk1.8.0_40/jre/bin/java
# VM options: <none>
# Warmup: 10 iterations, 5 s each
# Measurement: 30 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.jeffreybosboom.stringbuilderbench.StringBuilderBench.smart
# Run progress: 75.00% complete, ETA 00:06:49
# Fork: 1 of 1
# Warmup Iteration 1: 5208.142 ops/s
# Warmup Iteration 2: 5582.418 ops/s
# Warmup Iteration 3: 6173.640 ops/s
# Warmup Iteration 4: 4740.203 ops/s
# Warmup Iteration 5: 5144.127 ops/s
# Warmup Iteration 6: 4916.250 ops/s
# Warmup Iteration 7: 5870.128 ops/s
# Warmup Iteration 8: 6027.848 ops/s
# Warmup Iteration 9: 5993.312 ops/s
# Warmup Iteration 10: 6024.028 ops/s
Iteration 1: 6031.430 ops/s
Iteration 2: 4871.233 ops/s
Iteration 3: 4817.737 ops/s
Iteration 4: 5097.049 ops/s
Iteration 5: 4862.744 ops/s
Iteration 6: 4926.390 ops/s
Iteration 7: 5779.711 ops/s
Iteration 8: 4998.130 ops/s
Iteration 9: 6004.556 ops/s
Iteration 10: 5543.246 ops/s
Iteration 11: 6049.319 ops/s
Iteration 12: 6072.734 ops/s
Iteration 13: 6252.073 ops/s
Iteration 14: 6240.111 ops/s
Iteration 15: 6241.159 ops/s
Iteration 16: 5063.071 ops/s
Iteration 17: 4834.826 ops/s
Iteration 18: 4853.578 ops/s
Iteration 19: 5475.422 ops/s
Iteration 20: 6081.708 ops/s
Iteration 21: 6278.642 ops/s
Iteration 22: 4904.057 ops/s
Iteration 23: 5600.939 ops/s
Iteration 24: 5042.859 ops/s
Iteration 25: 5292.616 ops/s
Iteration 26: 5059.796 ops/s
Iteration 27: 5235.415 ops/s
Iteration 28: 5491.625 ops/s
Iteration 29: 4993.133 ops/s
Iteration 30: 5164.959 ops/s
Result: 5438.676 ±(99.9%) 352.088 ops/s [Average]
Statistics: (min, avg, max) = (4817.737, 5438.676, 6278.642), stdev = 526.989
Confidence interval (99.9%): [5086.588, 5790.764]
# JMH 1.6.2 (released 7 days ago)
# VM invoker: /data/scratch/jbosboom/jdk1.8.0_40/jre/bin/java
# VM options: <none>
# Warmup: 10 iterations, 5 s each
# Measurement: 30 iterations, 5 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.jeffreybosboom.stringbuilderbench.StringBuilderBench.smart3
# Run progress: 87.50% complete, ETA 00:03:24
# Fork: 1 of 1
# Warmup Iteration 1: 2938.131 ops/s
# Warmup Iteration 2: 2875.106 ops/s
# Warmup Iteration 3: 3092.491 ops/s
# Warmup Iteration 4: 2824.023 ops/s
# Warmup Iteration 5: 3330.853 ops/s
# Warmup Iteration 6: 3314.156 ops/s
# Warmup Iteration 7: 3275.936 ops/s
# Warmup Iteration 8: 2911.280 ops/s
# Warmup Iteration 9: 3391.046 ops/s
# Warmup Iteration 10: 3424.604 ops/s
Iteration 1: 3411.904 ops/s
Iteration 2: 3426.183 ops/s
Iteration 3: 3423.284 ops/s
Iteration 4: 3436.536 ops/s
Iteration 5: 3443.747 ops/s
Iteration 6: 3049.587 ops/s
Iteration 7: 3413.533 ops/s
Iteration 8: 2913.896 ops/s
Iteration 9: 3348.923 ops/s
Iteration 10: 3408.199 ops/s
Iteration 11: 3418.061 ops/s
Iteration 12: 3373.853 ops/s
Iteration 13: 3426.152 ops/s
Iteration 14: 3423.919 ops/s
Iteration 15: 3408.160 ops/s
Iteration 16: 3414.307 ops/s
Iteration 17: 3393.305 ops/s
Iteration 18: 3396.163 ops/s
Iteration 19: 3416.338 ops/s
Iteration 20: 3418.330 ops/s
Iteration 21: 3410.160 ops/s
Iteration 22: 3421.332 ops/s
Iteration 23: 3412.688 ops/s
Iteration 24: 3417.404 ops/s
Iteration 25: 3375.491 ops/s
Iteration 26: 3409.398 ops/s
Iteration 27: 3406.259 ops/s
Iteration 28: 2929.081 ops/s
Iteration 29: 2944.708 ops/s
Iteration 30: 2959.131 ops/s
Result: 3335.001 ±(99.9%) 115.600 ops/s [Average]
Statistics: (min, avg, max) = (2913.896, 3335.001, 3443.747), stdev = 173.025
Confidence interval (99.9%): [3219.401, 3450.601]
# Run complete. Total time: 00:27:08
Benchmark Mode Cnt Score Error Units
StringBuilderBench.concat thrpt 30 9.685 ± 0.924 ops/s
StringBuilderBench.concat3 thrpt 30 3.182 ± 0.228 ops/s
StringBuilderBench.explicit thrpt 30 9.078 ± 0.884 ops/s
StringBuilderBench.explicit3 thrpt 30 8.597 ± 0.237 ops/s
StringBuilderBench.implicit thrpt 30 10.290 ± 0.878 ops/s
StringBuilderBench.implicit3 thrpt 30 9.303 ± 0.838 ops/s
StringBuilderBench.smart thrpt 30 5438.676 ± 352.088 ops/s
StringBuilderBench.smart3 thrpt 30 3335.001 ± 115.600 ops/s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment