Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/commonMain/kotlin/microBenchmarks/StringBenchmark.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,48 @@ class StringBenchmark {
}
return sum
}

@Benchmark
fun stringBuilderCompareWithInterpolation(): Int {
var sum = 0
var i = 0
var j = 0
while (i < BENCHMARK_SIZE) {
val add1 = stringsInterpolation[j]
j = (j + 1) and 15
val add2 = stringsInterpolation[j]
j = (j + 1) and 15
val add3 = stringsInterpolation[j]
j = (j + 1) and 15
val builder = StringBuilder(add1)
builder.append(add2)
builder.append(add3)
val string = builder.toString()
sum += string.length
i++
}
return sum
}

@Benchmark
fun stringPlusCompareWithInterpolation(): Int {
var sum = 0
var i = 0
var j = 0
while (i < BENCHMARK_SIZE) {
val add1 = stringsInterpolation[j]
j = (j + 1) and 15
val add2 = stringsInterpolation[j]
j = (j + 1) and 15
val add3 = stringsInterpolation[j]
j = (j + 1) and 15
var string = add1
string = string.plus(add2)
string = string.plus(add3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd change it into add1 + add2 + add3

sum += string.length
i++
}
return sum
}

}