diff --git a/examples/src/benchmarks/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt b/examples/src/main/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt similarity index 70% rename from examples/src/benchmarks/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt rename to examples/src/main/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt index c5474e1d2..17a70a4aa 100644 --- a/examples/src/benchmarks/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt +++ b/examples/src/main/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt @@ -1,21 +1,16 @@ package scientifik.kmath.ast -import org.openjdk.jmh.annotations.Benchmark -import org.openjdk.jmh.annotations.Scope -import org.openjdk.jmh.annotations.State import scientifik.kmath.asm.compile import scientifik.kmath.expressions.Expression import scientifik.kmath.expressions.expressionInField +import scientifik.kmath.expressions.invoke import scientifik.kmath.operations.Field import scientifik.kmath.operations.RealField import kotlin.random.Random +import kotlin.system.measureTimeMillis -@State(Scope.Benchmark) class ExpressionsInterpretersBenchmark { private val algebra: Field = RealField - private val random: Random = Random(1) - - @Benchmark fun functionalExpression() { val expr = algebra.expressionInField { variable("x") * const(2.0) + const(2.0) / variable("x") - const(16.0) @@ -24,7 +19,6 @@ class ExpressionsInterpretersBenchmark { invokeAndSum(expr) } - @Benchmark fun mstExpression() { val expr = algebra.mstInField { symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) @@ -33,7 +27,6 @@ class ExpressionsInterpretersBenchmark { invokeAndSum(expr) } - @Benchmark fun asmExpression() { val expr = algebra.mstInField { symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) @@ -43,6 +36,7 @@ class ExpressionsInterpretersBenchmark { } private fun invokeAndSum(expr: Expression) { + val random = Random(0) var sum = 0.0 repeat(1000000) { @@ -52,3 +46,25 @@ class ExpressionsInterpretersBenchmark { println(sum) } } + +fun main() { + val benchmark = ExpressionsInterpretersBenchmark() + + val fe = measureTimeMillis { + benchmark.functionalExpression() + } + + println("fe=$fe") + + val mst = measureTimeMillis { + benchmark.mstExpression() + } + + println("mst=$mst") + + val asm = measureTimeMillis { + benchmark.asmExpression() + } + + println("asm=$asm") +}