Skip to content

Instantly share code, notes, and snippets.

@jonas
Created January 2, 2017 19:08
Show Gist options
  • Save jonas/f45c2dce32297f442084d7ff60db3896 to your computer and use it in GitHub Desktop.
Save jonas/f45c2dce32297f442084d7ff60db3896 to your computer and use it in GitHub Desktop.
Scala string concatenation benchmark
[info] Benchmark Mode Cnt Score Error Units
[info] StringConcatenationBenchmark.stringBuilder thrpt 20 24401716.742 ± 759067.927 ops/s
[info] StringConcatenationBenchmark.stringBuilderViaVarargs thrpt 20 11990758.447 ± 942632.452 ops/s
[info] StringConcatenationBenchmark.stringConcat thrpt 20 30990302.536 ± 2168504.239 ops/s
[info] StringConcatenationBenchmark.stringConcatFn thrpt 20 31361529.780 ± 2022207.795 ops/s
[info] StringConcatenationBenchmark.stringConcatVarArgs thrpt 20 7348008.964 ± 234634.594 ops/s
[info] StringConcatenationBenchmark.stringConcatVarArgs2 thrpt 20 5662366.576 ± 115998.273 ops/s
[info] StringConcatenationBenchmark.stringContext thrpt 20 7612447.788 ± 451671.634 ops/s
[info] StringConcatenationBenchmark.stringFormat thrpt 20 901011.458 ± 22039.147 ops/s
[info] StringConcatenationBenchmark.stringMkString thrpt 20 4748503.980 ± 85292.496 ops/s
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
/**
* The following command will run the benchmarks with reasonable settings:
*
* > sbt "benchmark/jmh:run -i 10 -wi 10 -f 2 -t 1 StringConcatenationBenchmark"
*/
@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
class StringConcatenationBenchmark {
def times(s: String)(i: Int) = s
def text = "asdf"
@Benchmark
def stringFormat = String.format("%s%s%s", times("a")(42), text, times("c")(43))
@Benchmark
def stringContext = s"${times("a")(42)}$text${times("c")(43)}"
@Benchmark
def stringConcat: String = times("a")(42).concat(text).concat(times("c")(43))
@Benchmark
def stringConcatFn: String = stringConcatter(times("a")(42), text, times("c")(43))
@inline def stringConcatter(a: String, b: String, c: String) = {
a.concat(b).concat(c)
}
@Benchmark
def stringConcatVarArgs: String = stringConcatVarargs(times("a")(42), text, times("c")(43))
@inline def stringConcatVarargs(ss: String*) = {
ss.fold("") { case (l, r) => l.concat(r) }
}
@Benchmark
def stringConcatVarArgs2: String = stringConcatVarargs2(times("a")(42), text, times("c")(43))
@inline def stringConcatVarargs2(ss: String*) = {
var s = ss.head
for (x <- ss.tail)
s = s.concat(x)
s
}
@Benchmark
def stringMkString: String = Seq(times("a")(42), text, times("c")(43)).mkString
@Benchmark
def stringBuilder: String = new StringBuilder(times("a")(42)).append(text).append(times("c")(43)).toString
@Benchmark
def stringBuilderViaVarargs: String = stringBuilderFromVarargs(times("a")(42), text, times("c")(43))
def stringBuilderFromVarargs(ss: String*) = {
val sb = new StringBuilder
ss.foreach(s => sb.append(s))
sb.toString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment