Created
May 20, 2025 17:23
-
-
Save lppedd/592c7b4d7efc93086ba4e45650338845 to your computer and use it in GitHub Desktop.
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 characters
@State(Scope.Benchmark) | |
@BenchmarkMode(Mode.Throughput) | |
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS) | |
@Warmup(iterations = 4, time = 10, timeUnit = BenchmarkTimeUnit.SECONDS) | |
@Measurement(iterations = 4, time = 10, timeUnit = BenchmarkTimeUnit.SECONDS) | |
public class TypedArrayBenchmark { | |
private var fromArray = ByteArray(0) | |
@Param("100", "1000", "5000", "15000", "100000") | |
public var newSize: Int = 0 | |
@Setup | |
public fun setup() { | |
val size = 10000 | |
val range = -127..127 | |
val array = ByteArray(size) | |
for (i in 0..<size) { | |
array[i] = range.random().toByte() | |
} | |
fromArray = array | |
} | |
@Benchmark | |
public fun fillFrom(blackhole: Blackhole) { | |
val toArray = ByteArray(newSize) | |
val srcLen = fromArray.size | |
val dstLen = toArray.size | |
var index = 0 | |
val from = fromArray.unsafeCast<Array<Any?>>() | |
val to = toArray.unsafeCast<Array<Any?>>() | |
while (index < srcLen && index < dstLen) { | |
to[index] = from[index++] | |
} | |
blackhole.consume(to) | |
} | |
@Benchmark | |
public fun typedArraySet(blackhole: Blackhole) { | |
if (newSize > fromArray.size) { | |
val toArray = ByteArray(newSize) | |
toArray.asDynamic().set(fromArray, 0) | |
blackhole.consume(toArray) | |
} else { | |
val toArray = fromArray.asDynamic().slice(0, newSize) | |
blackhole.consume(toArray.unsafeCast<Any>()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment