diff --git a/examples/src/main/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt b/examples/src/main/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt index 069fb628e..d7c97a96b 100644 --- a/examples/src/main/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt +++ b/examples/src/main/kotlin/scientifik/kmath/bignum/BigIntegerBenchmark.kt @@ -1,7 +1,7 @@ package scientifik.kmath.bignum -import com.ionspin.kotlin.bignum.integer.BigInteger import scientifik.kmath.operations.BigIntField +import scientifik.kmath.operations.JBigIntegerField import scientifik.kmath.operations.invoke import kotlin.concurrent.thread import kotlin.random.Random @@ -9,30 +9,26 @@ import kotlin.system.measureTimeMillis private class BigIntegerBenchmark { fun java() { - invokeAndSum { l, l2 -> (l.toBigInteger() * l2.toBigInteger()).toLong() } + val random = Random(0) + var sum = JBigIntegerField.zero + repeat(1000000) { + sum += JBigIntegerField { number(random.nextInt()) * random.nextInt() } + } + println("java:$sum") } fun bignum() { - invokeAndSum { l, l2 -> (BigInteger.fromLong(l) * BigInteger.fromLong(l2)).longValue() } + val random = Random(0) + var sum = BigIntegerRing.zero + repeat(1000000) { sum += BigIntegerRing { number(random.nextInt()) * random.nextInt() } } + println("bignum:$sum") } fun bigint() { - invokeAndSum { l, l2 -> BigIntField { number(l) * number(l2) }.toString().toLong() } - } - - fun long() { - invokeAndSum { l, l2 -> l * l2 } - } - - private fun invokeAndSum(op: (Long, Long) -> Long) { val random = Random(0) - var sum = 0.0 - - repeat(1000000) { - sum += op(random.nextInt().toLong(), random.nextInt().toLong()) - } - - println(sum) + var sum = BigIntField.zero + repeat(1000000) { sum += BigIntField { number(random.nextInt()) * random.nextInt() } } + println("bigint:$sum") } } @@ -53,9 +49,4 @@ fun main() { val bigint = measureTimeMillis(benchmark::bigint) println("bigint=$bigint") } - - thread { - val long = measureTimeMillis(benchmark::long) - println("long=$long") - } } 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 f32ed61eb..d99d4934b 100644 --- a/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigIntegerRing.kt +++ b/kmath-bignum/src/commonMain/kotlin/scientifik/kmath/bignum/BigIntegerRing.kt @@ -3,7 +3,7 @@ package scientifik.kmath.bignum import com.ionspin.kotlin.bignum.integer.BigInteger import scientifik.kmath.operations.Ring -class BigIntegerRing : Ring { +object BigIntegerRing : Ring { override val zero: BigInteger get() = BigInteger.ZERO