diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 3f3c06764..26fa9e76b 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -36,8 +36,8 @@ dependencies { implementation(project(":kmath-memory")) 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.benchmark.runtime:0.2.0-dev-8") - "benchmarksCompile"(sourceSets.main.get().output + sourceSets.main.get().compileClasspath) //sourceSets.main.output + sourceSets.main.runtimeClasspath + "benchmarksImplementation"("org.jetbrains.kotlinx:kotlinx.benchmark.runtime-jvm:0.2.0-dev-8") + "benchmarksImplementation"(sourceSets.main.get().output.plus(sourceSets.main.get().runtimeClasspath)) } // Configure benchmark diff --git a/examples/src/benchmarks/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt b/examples/src/benchmarks/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt new file mode 100644 index 000000000..fca4e22ba --- /dev/null +++ b/examples/src/benchmarks/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt @@ -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") + } +} diff --git a/examples/src/main/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt b/examples/src/main/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt deleted file mode 100644 index d7c97a96b..000000000 --- a/examples/src/main/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt +++ /dev/null @@ -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") - } -} diff --git a/kmath-bignum/build.gradle.kts b/kmath-bignum/build.gradle.kts index cb23be68b..659e71208 100644 --- a/kmath-bignum/build.gradle.kts +++ b/kmath-bignum/build.gradle.kts @@ -3,7 +3,7 @@ plugins { id("scientifik.mpp") } kotlin.sourceSets { commonMain { dependencies { - api("com.ionspin.kotlin:bignum:0.2.0") + api("com.ionspin.kotlin:bignum:0.1.5") api(project(":kmath-core")) } } diff --git a/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigIntegerRing.kt b/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigIntegerRing.kt index d99d4934b..3608256b9 100644 --- a/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigIntegerRing.kt +++ b/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigIntegerRing.kt @@ -14,8 +14,6 @@ object BigIntegerRing : Ring { 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, 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.minus(b: Number): BigInteger = minus(number(b)) override fun BigInteger.div(k: Number): BigInteger = divide(number(k))