Scala StringBuilder vs Java StringBuilder Performance

@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(value = 2, jvmArgs = Array("-Xmx2G"))
@Measurement(iterations = 7, time = 3, timeUnit = TimeUnit.SECONDS)
@Warmup(iterations = 3, time = 3, timeUnit = TimeUnit.SECONDS)
class StringBuilderBenchmark {

@Benchmark
def javaStringBuilder: String = {
new java.lang.StringBuilder().append("abc").append("def")
.toString
}

@Benchmark
def javaStringBuilder2: String = {
new java.lang.StringBuilder().append(495-char-length-string).append(495-char-length-string).toString
}
@Benchmark
def scalaStringBuilder: String = {
new scala.collection.mutable.StringBuilder().append("abc")
.append("def").toString
}
@Benchmark
def scalaStringBuilder: String = {
new scala.collection.mutable.StringBuilder().append(495-char-length-string).append(495-char-length-string).toString
}
}
Benchmark            Mode  Cnt    Score   Error  Units
javaStringBuilder avgt 14 8.754 ± 0.465 ns/op
javaStringBuilder2 avgt 14 237.280 ± 0.828 ns/op
scalaStringBuilder avgt 14 27.299 ± 0.096 ns/op
scalaStringBuilder2 avgt 14 720.742 ± 3.528 ns/op

--

--

Software developer, moved to Israel from Russia, trying to be aware of things.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dmitry Komanov

Dmitry Komanov

Software developer, moved to Israel from Russia, trying to be aware of things.