Skip to content

Commit 051068c

Browse files
committed
benchmarks with StringBuilder and String.plus similar to string interpolation benchmark
1 parent e729ddb commit 051068c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/commonMain/kotlin/microBenchmarks/StringBenchmark.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,48 @@ class StringBenchmark {
143143
}
144144
return sum
145145
}
146+
147+
@Benchmark
148+
fun stringBuilderCompareWithInterpolation(): Int {
149+
var sum = 0
150+
var i = 0
151+
var j = 0
152+
while (i < BENCHMARK_SIZE) {
153+
val add1 = stringsInterpolation[j]
154+
j = (j + 1) and 15
155+
val add2 = stringsInterpolation[j]
156+
j = (j + 1) and 15
157+
val add3 = stringsInterpolation[j]
158+
j = (j + 1) and 15
159+
val builder = StringBuilder(add1)
160+
builder.append(add2)
161+
builder.append(add3)
162+
val string = builder.toString()
163+
sum += string.length
164+
i++
165+
}
166+
return sum
167+
}
168+
169+
@Benchmark
170+
fun stringPlusCompareWithInterpolation(): Int {
171+
var sum = 0
172+
var i = 0
173+
var j = 0
174+
while (i < BENCHMARK_SIZE) {
175+
val add1 = stringsInterpolation[j]
176+
j = (j + 1) and 15
177+
val add2 = stringsInterpolation[j]
178+
j = (j + 1) and 15
179+
val add3 = stringsInterpolation[j]
180+
j = (j + 1) and 15
181+
var string = add1
182+
string = string.plus(add2)
183+
string = string.plus(add3)
184+
sum += string.length
185+
i++
186+
}
187+
return sum
188+
}
189+
146190
}

0 commit comments

Comments
 (0)