From a110dc20d5acf61c40596e98bf7ea3b9b9d0ba66 Mon Sep 17 00:00:00 2001 From: zhelenskiy Date: Fri, 16 Apr 2021 22:15:12 +0300 Subject: [PATCH 1/5] 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 -- 2.34.1 From 41d0be8085849304758d832ff419aa8f431aa93e Mon Sep 17 00:00:00 2001 From: zhelenskiy Date: Fri, 16 Apr 2021 23:29:31 +0300 Subject: [PATCH 2/5] Increasing number of tests --- .../kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 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 31fafaf75..e988103dd 100644 --- a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt +++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt @@ -18,10 +18,10 @@ 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() + number(11).pow10().pow10().pow10().pow10().pow10() } val jvmNumber = JBigIntegerField.number(Int.MAX_VALUE) - val largeJvmNumber = JBigIntegerField { number(11).pow(1000) } + val largeJvmNumber = JBigIntegerField { number(11).pow(100_000) } val bigExponent = 50_000 @Benchmark -- 2.34.1 From 562e641b06b72639b12a6a88e4be4b698e305c13 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 17 Apr 2021 09:43:40 +0300 Subject: [PATCH 3/5] Fix benchmarks --- .../kmath/benchmarks/BigIntBenchmark.kt | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt index e988103dd..4b19f8a63 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt @@ -5,23 +5,26 @@ 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.* +import space.kscience.kmath.operations.BigInt +import space.kscience.kmath.operations.BigIntField +import space.kscience.kmath.operations.JBigIntegerField +import space.kscience.kmath.operations.invoke + +private fun BigInt.pow(power: Int): BigInt = modPow(BigIntField.number(power), BigInt.ONE) + @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().pow10().pow10() - } val jvmNumber = JBigIntegerField.number(Int.MAX_VALUE) - val largeJvmNumber = JBigIntegerField { number(11).pow(100_000) } + val largeKmNumber = BigIntField { number(11).pow(100_000) } + val largeJvmNumber = JBigIntegerField { number(11).pow(100_000) } val bigExponent = 50_000 @Benchmark @@ -41,7 +44,7 @@ internal class BigIntBenchmark { @Benchmark fun kmMultiplyLarge(blackhole: Blackhole) = BigIntField { - blackhole.consume(largeKmNumber.let { it * it }) + blackhole.consume(largeKmNumber*largeKmNumber) } @Benchmark @@ -51,12 +54,12 @@ internal class BigIntBenchmark { @Benchmark fun jvmMultiplyLarge(blackhole: Blackhole) = JBigIntegerField { - blackhole.consume(largeJvmNumber.let { it * it }) + blackhole.consume(largeJvmNumber*largeJvmNumber) } @Benchmark fun kmPower(blackhole: Blackhole) = BigIntField { - blackhole.consume(kmNumber.power(bigExponent, ::multiply)) + blackhole.consume(kmNumber.pow(bigExponent)) } @Benchmark -- 2.34.1 From 3dc7038b6edac589da72ece0c135b93f62f2cb54 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 17 Apr 2021 09:46:10 +0300 Subject: [PATCH 4/5] Update BigIntBenchmark.kt unify definition (pow function seems to work wrong --- .../kmath/benchmarks/BigIntBenchmark.kt | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 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 e988103dd..21222ddd3 100644 --- a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt +++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt @@ -12,16 +12,17 @@ import org.openjdk.jmh.annotations.Scope import org.openjdk.jmh.annotations.State import space.kscience.kmath.operations.* + +private fun BigInt.pow(power: Int): BigInt = modPow(BigIntField.number(power), BigInt.ONE) + + @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().pow10().pow10() - } val jvmNumber = JBigIntegerField.number(Int.MAX_VALUE) - val largeJvmNumber = JBigIntegerField { number(11).pow(100_000) } + val largeKmNumber = BigIntField { number(11).pow(100_000) } + val largeJvmNumber = JBigIntegerField { number(11).pow(100_000) } val bigExponent = 50_000 @Benchmark @@ -41,7 +42,7 @@ internal class BigIntBenchmark { @Benchmark fun kmMultiplyLarge(blackhole: Blackhole) = BigIntField { - blackhole.consume(largeKmNumber.let { it * it }) + blackhole.consume(largeKmNumber*largeKmNumber) } @Benchmark @@ -51,16 +52,16 @@ internal class BigIntBenchmark { @Benchmark fun jvmMultiplyLarge(blackhole: Blackhole) = JBigIntegerField { - blackhole.consume(largeJvmNumber.let { it * it }) + blackhole.consume(largeJvmNumber*largeJvmNumber) } @Benchmark fun kmPower(blackhole: Blackhole) = BigIntField { - blackhole.consume(kmNumber.power(bigExponent, ::multiply)) + blackhole.consume(kmNumber.pow(bigExponent)) } @Benchmark fun jvmPower(blackhole: Blackhole) = JBigIntegerField { blackhole.consume(jvmNumber.pow(bigExponent)) } -} \ No newline at end of file +} -- 2.34.1 From f67cfcc9e605106e6744d7c50cdee6d02c58065e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 17 Apr 2021 09:47:34 +0300 Subject: [PATCH 5/5] Fix benchmarks --- .../kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt index 66c034d06..b9a10c774 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt @@ -18,10 +18,6 @@ import space.kscience.kmath.operations.invoke private fun BigInt.pow(power: Int): BigInt = modPow(BigIntField.number(power), BigInt.ONE) - -private fun BigInt.pow(power: Int): BigInt = modPow(BigIntField.number(power), BigInt.ONE) - - @State(Scope.Benchmark) internal class BigIntBenchmark { -- 2.34.1