Skip to content

Commit ff8f3d1

Browse files
committed
update String benchmarks & d8 version
1 parent be9ae7f commit ff8f3d1

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ apply<BinaryenRootPlugin>()
5353
the<BinaryenRootEnvSpec>().version.set("123")
5454

5555
apply<D8Plugin>()
56-
the<D8EnvSpec>().version.set("13.4.61")
56+
the<D8EnvSpec>().version.set("14.2.133")
5757

5858
allprojects.forEach {
5959
it.tasks.withType<org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask>().configureEach {
@@ -138,6 +138,7 @@ benchmark {
138138
"microBenchmarks.ArrayCopyBenchmark.copyInSameArray",
139139
"microBenchmarks.BunnymarkBenchmark.testBunnymark",
140140
"microBenchmarks.CoordinatesSolverBenchmark.solve",
141+
"microBenchmarks.StringBenchmark.subSequence",
141142
)
142143
with(create("fastMicro")) {
143144
iterations = 5

src/commonMain/kotlin/microBenchmarks/StringBenchmark.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
package microBenchmarks
1818

1919
import kotlinx.benchmark.*
20+
import kotlin.math.roundToInt
21+
import kotlin.math.sqrt
2022

2123
@State(Scope.Benchmark)
2224
class StringBenchmark {
2325
private var csv: String = ""
26+
private val subSequenceRanges = mutableListOf<Pair<Int, Int>>()
2427

2528
@Setup
2629
fun setup() {
@@ -30,6 +33,12 @@ class StringBenchmark {
3033
csv += ","
3134
}
3235
csv += 0.0
36+
37+
for (i in 1..sqrt(BENCHMARK_SIZE.toDouble()).roundToInt()) {
38+
for (j in 0..csv.length - i) {
39+
subSequenceRanges.add(Pair(j, j + i))
40+
}
41+
}
3342
}
3443

3544
@Benchmark
@@ -92,4 +101,23 @@ class StringBenchmark {
92101
}
93102
return sum
94103
}
104+
105+
@Benchmark
106+
fun iterateCharsCsv(): Int {
107+
var sum = 0
108+
for (i in 0 until BENCHMARK_SIZE) {
109+
sum += csv[i].code
110+
}
111+
return sum
112+
}
113+
114+
@Benchmark
115+
fun subSequenceCsv(): Int {
116+
var sum = 0
117+
for (range in subSequenceRanges) {
118+
val subString = csv.subSequence(range.first, range.second)
119+
sum += subString[0].code
120+
}
121+
return sum
122+
}
95123
}

0 commit comments

Comments
 (0)