Fix benchmarks

This commit is contained in:
Iaroslav Postovalov 2020-09-13 16:18:35 +07:00
parent 3dc59ccd26
commit dc02d957b0
No known key found for this signature in database
GPG Key ID: 70D5F4DCB0972F1B
5 changed files with 41 additions and 57 deletions

View File

@ -36,8 +36,8 @@ dependencies {
implementation(project(":kmath-memory")) implementation(project(":kmath-memory"))
implementation("com.kyonifer:koma-core-ejml:0.12") implementation("com.kyonifer:koma-core-ejml:0.12")
implementation("org.jetbrains.kotlinx:kotlinx-io-jvm:0.2.0-npm-dev-6") implementation("org.jetbrains.kotlinx:kotlinx-io-jvm:0.2.0-npm-dev-6")
implementation("org.jetbrains.kotlinx:kotlinx.benchmark.runtime:0.2.0-dev-8") "benchmarksImplementation"("org.jetbrains.kotlinx:kotlinx.benchmark.runtime-jvm:0.2.0-dev-8")
"benchmarksCompile"(sourceSets.main.get().output + sourceSets.main.get().compileClasspath) //sourceSets.main.output + sourceSets.main.runtimeClasspath "benchmarksImplementation"(sourceSets.main.get().output.plus(sourceSets.main.get().runtimeClasspath))
} }
// Configure benchmark // Configure benchmark

View File

@ -0,0 +1,38 @@
package scientifik.kmath.bignum
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.State
import scientifik.kmath.operations.BigIntField
import scientifik.kmath.operations.JBigIntegerField
import scientifik.kmath.operations.invoke
import kotlin.random.Random
@State(Scope.Benchmark)
class BigIntegerBenchmark {
var times: Int = 1000000
@Benchmark
fun java() {
val random = Random(0)
var sum = JBigIntegerField.zero
repeat(times) { sum += JBigIntegerField { number(random.nextInt()) * random.nextInt() } }
println("java:$sum")
}
@Benchmark
fun bignum() {
val random = Random(0)
var sum = BigIntegerRing.zero
repeat(times) { sum += BigIntegerRing { number(random.nextInt()) * random.nextInt() } }
println("bignum:$sum")
}
@Benchmark
fun bigint() {
val random = Random(0)
var sum = BigIntField.zero
repeat(times) { sum += BigIntField { number(random.nextInt()) * random.nextInt() } }
println("bigint:$sum")
}
}

View File

@ -1,52 +0,0 @@
package scientifik.kmath.bignum
import scientifik.kmath.operations.BigIntField
import scientifik.kmath.operations.JBigIntegerField
import scientifik.kmath.operations.invoke
import kotlin.concurrent.thread
import kotlin.random.Random
import kotlin.system.measureTimeMillis
private class BigIntegerBenchmark {
fun java() {
val random = Random(0)
var sum = JBigIntegerField.zero
repeat(1000000) {
sum += JBigIntegerField { number(random.nextInt()) * random.nextInt() }
}
println("java:$sum")
}
fun bignum() {
val random = Random(0)
var sum = BigIntegerRing.zero
repeat(1000000) { sum += BigIntegerRing { number(random.nextInt()) * random.nextInt() } }
println("bignum:$sum")
}
fun bigint() {
val random = Random(0)
var sum = BigIntField.zero
repeat(1000000) { sum += BigIntField { number(random.nextInt()) * random.nextInt() } }
println("bigint:$sum")
}
}
fun main() {
val benchmark = BigIntegerBenchmark()
thread {
val java = measureTimeMillis(benchmark::java)
println("java=$java")
}
thread {
val bignum = measureTimeMillis(benchmark::bignum)
println("bignum=$bignum")
}
thread {
val bigint = measureTimeMillis(benchmark::bigint)
println("bigint=$bigint")
}
}

View File

@ -3,7 +3,7 @@ plugins { id("scientifik.mpp") }
kotlin.sourceSets { kotlin.sourceSets {
commonMain { commonMain {
dependencies { dependencies {
api("com.ionspin.kotlin:bignum:0.2.0") api("com.ionspin.kotlin:bignum:0.1.5")
api(project(":kmath-core")) api(project(":kmath-core"))
} }
} }

View File

@ -14,8 +14,6 @@ object BigIntegerRing : Ring<BigInteger> {
override fun add(a: BigInteger, b: BigInteger): BigInteger = a + b override fun add(a: BigInteger, b: BigInteger): BigInteger = a + b
override fun multiply(a: BigInteger, k: Number): BigInteger = a * (number(k)) override fun multiply(a: BigInteger, k: Number): BigInteger = a * (number(k))
override fun multiply(a: BigInteger, b: BigInteger): BigInteger = a * b override fun multiply(a: BigInteger, b: BigInteger): BigInteger = a * b
override fun BigInteger.unaryMinus(): BigInteger = negate()
override fun BigInteger.minus(b: BigInteger): BigInteger = minus(b)
override fun BigInteger.plus(b: Number): BigInteger = plus(number(b)) override fun BigInteger.plus(b: Number): BigInteger = plus(number(b))
override fun BigInteger.minus(b: Number): BigInteger = minus(number(b)) override fun BigInteger.minus(b: Number): BigInteger = minus(number(b))
override fun BigInteger.div(k: Number): BigInteger = divide(number(k)) override fun BigInteger.div(k: Number): BigInteger = divide(number(k))