From 675ace272c06f5bada1640d8c9219687f3e1ee91 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Tue, 23 Jun 2020 03:38:20 +0700 Subject: [PATCH] Minor Gradle settings modification, add benchmarks of different Expression implementatinos --- examples/build.gradle.kts | 10 ++-- .../ast/ExpressionsInterpretersBenchmark.kt | 54 +++++++++++++++++++ .../kmath/structures/ViktorBenchmark.kt | 17 ++---- settings.gradle.kts | 2 + 4 files changed, 64 insertions(+), 19 deletions(-) create mode 100644 examples/src/benchmarks/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 2fab47ac0..eadfc3f6b 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -1,16 +1,13 @@ -import org.jetbrains.kotlin.allopen.gradle.AllOpenExtension import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { java kotlin("jvm") - kotlin("plugin.allopen") version "1.3.71" - id("kotlinx.benchmark") version "0.2.0-dev-7" + kotlin("plugin.allopen") + id("kotlinx.benchmark") } -configure { - annotation("org.openjdk.jmh.annotations.State") -} +allOpen.annotation("org.openjdk.jmh.annotations.State") repositories { maven("http://dl.bintray.com/kyonifer/maven") @@ -24,6 +21,7 @@ sourceSets { } dependencies { + implementation(project(":kmath-ast")) implementation(project(":kmath-core")) implementation(project(":kmath-coroutines")) implementation(project(":kmath-commons")) diff --git a/examples/src/benchmarks/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt b/examples/src/benchmarks/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt new file mode 100644 index 000000000..c5474e1d2 --- /dev/null +++ b/examples/src/benchmarks/kotlin/scientifik/kmath/ast/ExpressionsInterpretersBenchmark.kt @@ -0,0 +1,54 @@ +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.operations.Field +import scientifik.kmath.operations.RealField +import kotlin.random.Random + +@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) + } + + invokeAndSum(expr) + } + + @Benchmark + fun mstExpression() { + val expr = algebra.mstInField { + symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) + } + + invokeAndSum(expr) + } + + @Benchmark + fun asmExpression() { + val expr = algebra.mstInField { + symbol("x") * number(2.0) + number(2.0) / symbol("x") - number(16.0) + }.compile() + + invokeAndSum(expr) + } + + private fun invokeAndSum(expr: Expression) { + var sum = 0.0 + + repeat(1000000) { + sum += expr("x" to random.nextDouble()) + } + + println(sum) + } +} diff --git a/examples/src/benchmarks/kotlin/scientifik/kmath/structures/ViktorBenchmark.kt b/examples/src/benchmarks/kotlin/scientifik/kmath/structures/ViktorBenchmark.kt index be4115d81..5dc166cd9 100644 --- a/examples/src/benchmarks/kotlin/scientifik/kmath/structures/ViktorBenchmark.kt +++ b/examples/src/benchmarks/kotlin/scientifik/kmath/structures/ViktorBenchmark.kt @@ -23,9 +23,7 @@ class ViktorBenchmark { fun `Automatic field addition`() { autoField.run { var res = one - repeat(n) { - res += 1.0 - } + repeat(n) { res += one } } } @@ -33,9 +31,7 @@ class ViktorBenchmark { fun `Viktor field addition`() { viktorField.run { var res = one - repeat(n) { - res += one - } + repeat(n) { res += one } } } @@ -43,9 +39,7 @@ class ViktorBenchmark { fun `Raw Viktor`() { val one = F64Array.full(init = 1.0, shape = *intArrayOf(dim, dim)) var res = one - repeat(n) { - res = res + one - } + repeat(n) { res = res + one } } @Benchmark @@ -53,10 +47,7 @@ class ViktorBenchmark { realField.run { val fortyTwo = produce { 42.0 } var res = one - - repeat(n) { - res = ln(fortyTwo) - } + repeat(n) { res = ln(fortyTwo) } } } diff --git a/settings.gradle.kts b/settings.gradle.kts index 465ecfca8..487e1d87f 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,10 +3,12 @@ pluginManagement { val toolsVersion = "0.5.0" plugins { + id("kotlinx.benchmark") version "0.2.0-dev-8" id("scientifik.mpp") version toolsVersion id("scientifik.jvm") version toolsVersion id("scientifik.atomic") version toolsVersion id("scientifik.publish") version toolsVersion + kotlin("plugin.allopen") version "1.3.72" } repositories {