Add basic benchmarks to compare different Expression implementations #113

Closed
CommanderTvis wants to merge 1 commits from adv-expr-benchmarks-2 into adv-expr
4 changed files with 64 additions and 19 deletions

View File

@ -1,16 +1,13 @@
import org.jetbrains.kotlin.allopen.gradle.AllOpenExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins { plugins {
java java
kotlin("jvm") kotlin("jvm")
kotlin("plugin.allopen") version "1.3.71" kotlin("plugin.allopen")
id("kotlinx.benchmark") version "0.2.0-dev-7" id("kotlinx.benchmark")
} }
configure<AllOpenExtension> { allOpen.annotation("org.openjdk.jmh.annotations.State")
annotation("org.openjdk.jmh.annotations.State")
}
repositories { repositories {
maven("http://dl.bintray.com/kyonifer/maven") maven("http://dl.bintray.com/kyonifer/maven")
@ -24,6 +21,7 @@ sourceSets {
} }
dependencies { dependencies {
implementation(project(":kmath-ast"))
implementation(project(":kmath-core")) implementation(project(":kmath-core"))
implementation(project(":kmath-coroutines")) implementation(project(":kmath-coroutines"))
implementation(project(":kmath-commons")) implementation(project(":kmath-commons"))

View File

@ -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<Double> = 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<Double>) {
var sum = 0.0
repeat(1000000) {
sum += expr("x" to random.nextDouble())
}
println(sum)
}
}

View File

@ -23,9 +23,7 @@ class ViktorBenchmark {
fun `Automatic field addition`() { fun `Automatic field addition`() {
autoField.run { autoField.run {
var res = one var res = one
repeat(n) { repeat(n) { res += one }
res += 1.0
}
} }
} }
@ -33,9 +31,7 @@ class ViktorBenchmark {
fun `Viktor field addition`() { fun `Viktor field addition`() {
viktorField.run { viktorField.run {
var res = one var res = one
repeat(n) { repeat(n) { res += one }
res += one
}
} }
} }
@ -43,9 +39,7 @@ class ViktorBenchmark {
fun `Raw Viktor`() { fun `Raw Viktor`() {
val one = F64Array.full(init = 1.0, shape = *intArrayOf(dim, dim)) val one = F64Array.full(init = 1.0, shape = *intArrayOf(dim, dim))
var res = one var res = one
repeat(n) { repeat(n) { res = res + one }
res = res + one
}
} }
@Benchmark @Benchmark
@ -53,10 +47,7 @@ class ViktorBenchmark {
realField.run { realField.run {
val fortyTwo = produce { 42.0 } val fortyTwo = produce { 42.0 }
var res = one var res = one
repeat(n) { res = ln(fortyTwo) }
repeat(n) {
res = ln(fortyTwo)
}
} }
} }

View File

@ -3,10 +3,12 @@ pluginManagement {
val toolsVersion = "0.5.0" val toolsVersion = "0.5.0"
plugins { plugins {
id("kotlinx.benchmark") version "0.2.0-dev-8"
id("scientifik.mpp") version toolsVersion id("scientifik.mpp") version toolsVersion
id("scientifik.jvm") version toolsVersion id("scientifik.jvm") version toolsVersion
id("scientifik.atomic") version toolsVersion id("scientifik.atomic") version toolsVersion
id("scientifik.publish") version toolsVersion id("scientifik.publish") version toolsVersion
kotlin("plugin.allopen") version "1.3.72"
} }
repositories { repositories {