|
| 1 | +package com.mapk.fastkfunction |
| 2 | + |
| 3 | +import com.mapk.fastkfunction.argumentbucket.ArgumentBucket |
| 4 | +import com.mapk.fastkfunction.benchmarktargets.Constructor5 |
| 5 | +import org.openjdk.jmh.annotations.Benchmark |
| 6 | +import org.openjdk.jmh.annotations.Scope |
| 7 | +import org.openjdk.jmh.annotations.State |
| 8 | +import java.lang.reflect.Constructor |
| 9 | +import kotlin.reflect.KFunction |
| 10 | +import kotlin.reflect.KParameter |
| 11 | +import kotlin.reflect.jvm.javaConstructor |
| 12 | + |
| 13 | +@State(Scope.Benchmark) |
| 14 | +open class CallConstructorBenchmark { |
| 15 | + private val function: KFunction<Constructor5> = ::Constructor5 |
| 16 | + private val argumentMap: Map<KParameter, Any?> = function.parameters.associateWith { it.index + 1 } |
| 17 | + |
| 18 | + private val javaConstructor: Constructor<Constructor5> = function.javaConstructor!! |
| 19 | + private val fastKFunction: FastKFunction<Constructor5> = FastKFunction(function, null) |
| 20 | + private val argumentBucket: ArgumentBucket = fastKFunction.generateBucket() |
| 21 | + .apply { (0 until 5).forEach { this[it] = it + 1 } } |
| 22 | + |
| 23 | + @Benchmark |
| 24 | + fun normalCall(): Constructor5 = Constructor5(1, 2, 3, 4, 5) |
| 25 | + |
| 26 | + @Benchmark |
| 27 | + fun kFunctionCall(): Constructor5 = function.call(1, 2, 3, 4, 5) |
| 28 | + |
| 29 | + @Benchmark |
| 30 | + fun kFunctionCallBy(): Constructor5 = function.callBy(argumentMap) |
| 31 | + |
| 32 | + @Benchmark |
| 33 | + fun javaConstructor(): Constructor5 = javaConstructor.newInstance(1, 2, 3, 4, 5) |
| 34 | + |
| 35 | + @Benchmark |
| 36 | + fun fastKFunctionCall(): Constructor5 = fastKFunction.call(1, 2, 3, 4, 5) |
| 37 | + |
| 38 | + @Benchmark |
| 39 | + fun fastKFunctionCallBy(): Constructor5 = fastKFunction.callBy(argumentBucket) |
| 40 | +} |
0 commit comments