I'm testing with the following class (You can find a git repository here):
@ExperimentalStdlibApi
@State(Scope.Benchmark)
class TestBenchmark {
private fun benchmark() : List<Int> {
return buildList {
addAll(0..100)
shuffle()
sortDescending()
}
}
final fun measureTime() {
val result: Any?
val time = measureNanoTime {
result = benchmark()
}
println("$time ns")
}
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
final fun benchmarkFunction() {
benchmark()
}
}
@ExperimentalStdlibApi
fun main() {
TestBenchmark().measureTime()
}
With kotlins measureTimeNanos
I get 65238400 ns
on my machine. However when performing a benchmark with kotlinx-benchmark via gradlew benchmark
I get:
Success: N = 611869
mean = 12388,465 ±(99.9%) 39,959 ns/op
How is that possible?