From a110dc20d5acf61c40596e98bf7ea3b9b9d0ba66 Mon Sep 17 00:00:00 2001 From: zhelenskiy Date: Fri, 16 Apr 2021 22:15:12 +0300 Subject: [PATCH] Tests for large BigIntegers multiplication and power are provided. Km implementation is very slow. --- .../kmath/benchmarks/BigIntBenchmark.kt | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt index 672efd5c2..31fafaf75 100644 --- a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt +++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt @@ -5,19 +5,24 @@ package space.kscience.kmath.benchmarks +import edu.mcgill.kaliningraph.power import kotlinx.benchmark.Blackhole import org.openjdk.jmh.annotations.Benchmark import org.openjdk.jmh.annotations.Scope import org.openjdk.jmh.annotations.State -import space.kscience.kmath.operations.BigIntField -import space.kscience.kmath.operations.JBigIntegerField -import space.kscience.kmath.operations.invoke +import space.kscience.kmath.operations.* @State(Scope.Benchmark) internal class BigIntBenchmark { val kmNumber = BigIntField.number(Int.MAX_VALUE) + val largeKmNumber = BigIntField { + fun BigInt.pow10(): BigInt = power(10, ::multiply) + number(11).pow10().pow10().pow10() + } val jvmNumber = JBigIntegerField.number(Int.MAX_VALUE) + val largeJvmNumber = JBigIntegerField { number(11).pow(1000) } + val bigExponent = 50_000 @Benchmark fun kmAdd(blackhole: Blackhole) = BigIntField { @@ -34,8 +39,28 @@ internal class BigIntBenchmark { blackhole.consume(kmNumber * kmNumber * kmNumber) } + @Benchmark + fun kmMultiplyLarge(blackhole: Blackhole) = BigIntField { + blackhole.consume(largeKmNumber.let { it * it }) + } + @Benchmark fun jvmMultiply(blackhole: Blackhole) = JBigIntegerField { blackhole.consume(jvmNumber * jvmNumber * jvmNumber) } + + @Benchmark + fun jvmMultiplyLarge(blackhole: Blackhole) = JBigIntegerField { + blackhole.consume(largeJvmNumber.let { it * it }) + } + + @Benchmark + fun kmPower(blackhole: Blackhole) = BigIntField { + blackhole.consume(kmNumber.power(bigExponent, ::multiply)) + } + + @Benchmark + fun jvmPower(blackhole: Blackhole) = JBigIntegerField { + blackhole.consume(jvmNumber.pow(bigExponent)) + } } \ No newline at end of file