Last active
March 11, 2026 23:34
-
-
Save lppedd/6548652d5687de69474033cb8d6caa8d to your computer and use it in GitHub Desktop.
Revisions
-
lppedd revised this gist
Mar 11, 2026 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,10 @@ import kotlinx.benchmark.* @State(Scope.Benchmark) @BenchmarkMode(Mode.Throughput) @Warmup(iterations = 4, time = 5, timeUnit = BenchmarkTimeUnit.SECONDS) @Measurement(iterations = 4, time = 5, timeUnit = BenchmarkTimeUnit.SECONDS) public class ByteArrayBenchmark { private val fromArray: ByteArray @Param("15", "100", "1000", "5000", "15000", "100000", "1000000") -
lppedd revised this gist
Mar 11, 2026 . 1 changed file with 1 addition and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,6 @@ import kotlinx.benchmark.* { private val fromArray: ByteArray @Param("15", "100", "1000", "5000", "15000", "100000", "1000000") -
lppedd created this gist
Mar 11, 2026 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ import kotlinx.benchmark.* @State(Scope.Benchmark) @BenchmarkMode(Mode.Throughput) @Warmup(iterations = 4, time = 5, timeUnit = BenchmarkTimeUnit.SECONDS) @Measurement(iterations = 4, time = 5, timeUnit = BenchmarkTimeUnit.SECONDS) public class ByteArrayToStringBenchmark { private val fromArray: ByteArray @Param("15", "100", "1000", "5000", "15000", "100000", "1000000") public var newSize: Int = 0 public constructor() { val size = 10000 val range = 0..255 val array = ByteArray(size) for (i in 0..<size) { array[i] = range.random().toByte() } fromArray = array } @Benchmark public fun typedArraySet(blackhole: Blackhole) { val size = fromArray.size val toArray = when { newSize < 16 || size < 16 -> ktFillFrom(fromArray, ByteArray(newSize)) newSize > size -> ByteArray(newSize).also { copy -> copy.asDynamic().set(fromArray, 0) } else -> fromArray.asDynamic().slice(0, newSize) } blackhole.consume(toArray.unsafeCast<Any>()) } @Benchmark public fun fillFrom(blackhole: Blackhole) { val toArray = ktFillFrom(fromArray, ByteArray(newSize)) blackhole.consume(toArray) } private fun ktFillFrom(src: dynamic, dst: dynamic): ByteArray { val srcLen: Int = src.length val dstLen: Int = dst.length var index: Int = 0 val arr = dst.unsafeCast<Array<Any?>>() while (index < srcLen && index < dstLen) { arr[index] = src[index] index++ } return dst } }