File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed
src/commonMain/kotlin/microBenchmarks Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ apply<BinaryenRootPlugin>()
5353the<BinaryenRootEnvSpec >().version.set(" 123" )
5454
5555apply<D8Plugin >()
56- the<D8EnvSpec >().version.set(" 13.4.61 " )
56+ the<D8EnvSpec >().version.set(" 14.2.133 " )
5757
5858allprojects.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
Original file line number Diff line number Diff line change 1717package microBenchmarks
1818
1919import kotlinx.benchmark.*
20+ import kotlin.math.roundToInt
21+ import kotlin.math.sqrt
2022
2123@State(Scope .Benchmark )
2224class 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}
You can’t perform that action at this time.
0 commit comments