|
| 1 | +/* |
| 2 | + * Copyright 2025 JetBrains s.r.o. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package microBenchmarks |
| 18 | + |
| 19 | +import kotlinx.benchmark.* |
| 20 | + |
| 21 | +private const val RUNS = 1_000_000 |
| 22 | + |
| 23 | +@State(Scope.Benchmark) |
| 24 | +class MultiFunctionInterfaceBenchmark { |
| 25 | + interface IFunc : |
| 26 | + Function<Int>, |
| 27 | + Function1<Int, Int>, |
| 28 | + Function2<Int, Int, Int>, |
| 29 | + Function3<Int, Int, Int, Int>, |
| 30 | + Function4<Int, Int, Int, Int, Int>, |
| 31 | + Function5<Int, Int, Int, Int, Int, Int> |
| 32 | + |
| 33 | + @State(Scope.Benchmark) |
| 34 | + class IFuncClass : IFunc { |
| 35 | + override fun invoke(p1: Int): Int = p1 |
| 36 | + |
| 37 | + override fun invoke(p1: Int, p2: Int): Int = p1 + p2 |
| 38 | + |
| 39 | + override fun invoke(p1: Int, p2: Int, p3: Int): Int = p1 + p2 + p3 |
| 40 | + |
| 41 | + override fun invoke(p1: Int, p2: Int, p3: Int, p4: Int): Int = p1 + p2 + p3 + p4 |
| 42 | + |
| 43 | + override fun invoke(p1: Int, p2: Int, p3: Int, p4: Int, p5: Int): Int = p1 + p2 + p3 + p4 + p5 |
| 44 | + } |
| 45 | + |
| 46 | + private lateinit var iFunc: IFunc |
| 47 | + |
| 48 | + @Setup |
| 49 | + fun setup() { |
| 50 | + iFunc = IFuncClass() |
| 51 | + } |
| 52 | + |
| 53 | + @Benchmark |
| 54 | + fun interfaceFunctionCall(): Int { |
| 55 | + val obj = iFunc |
| 56 | + var result = 0 |
| 57 | + var i = 0 |
| 58 | + while (i < RUNS) { |
| 59 | + result += obj(i) |
| 60 | + result += obj(i, i) |
| 61 | + result += obj(i, i, i) |
| 62 | + result += obj(i, i, i, i) |
| 63 | + result += obj(i, i, i, i, i) |
| 64 | + i++ |
| 65 | + } |
| 66 | + return result |
| 67 | + } |
| 68 | +} |
0 commit comments