Merge pull request #281 from zhelenskiy/dev

Tests for large BigIntegers multiplication and power are provided.
This commit is contained in:
Alexander Nozik 2021-04-17 09:56:18 +03:00 committed by GitHub
commit 2418b0bb9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,19 +5,27 @@
package space.kscience.kmath.benchmarks package space.kscience.kmath.benchmarks
import kotlinx.benchmark.Blackhole import kotlinx.benchmark.Blackhole
import org.openjdk.jmh.annotations.Benchmark import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.State import org.openjdk.jmh.annotations.State
import space.kscience.kmath.operations.BigInt
import space.kscience.kmath.operations.BigIntField import space.kscience.kmath.operations.BigIntField
import space.kscience.kmath.operations.JBigIntegerField import space.kscience.kmath.operations.JBigIntegerField
import space.kscience.kmath.operations.invoke import space.kscience.kmath.operations.invoke
private fun BigInt.pow(power: Int): BigInt = modPow(BigIntField.number(power), BigInt.ONE)
@State(Scope.Benchmark) @State(Scope.Benchmark)
internal class BigIntBenchmark { internal class BigIntBenchmark {
val kmNumber = BigIntField.number(Int.MAX_VALUE) val kmNumber = BigIntField.number(Int.MAX_VALUE)
val jvmNumber = JBigIntegerField.number(Int.MAX_VALUE) val jvmNumber = JBigIntegerField.number(Int.MAX_VALUE)
val largeKmNumber = BigIntField { number(11).pow(100_000) }
val largeJvmNumber = JBigIntegerField { number(11).pow(100_000) }
val bigExponent = 50_000
@Benchmark @Benchmark
fun kmAdd(blackhole: Blackhole) = BigIntField { fun kmAdd(blackhole: Blackhole) = BigIntField {
@ -34,8 +42,28 @@ internal class BigIntBenchmark {
blackhole.consume(kmNumber * kmNumber * kmNumber) blackhole.consume(kmNumber * kmNumber * kmNumber)
} }
@Benchmark
fun kmMultiplyLarge(blackhole: Blackhole) = BigIntField {
blackhole.consume(largeKmNumber*largeKmNumber)
}
@Benchmark @Benchmark
fun jvmMultiply(blackhole: Blackhole) = JBigIntegerField { fun jvmMultiply(blackhole: Blackhole) = JBigIntegerField {
blackhole.consume(jvmNumber * jvmNumber * jvmNumber) blackhole.consume(jvmNumber * jvmNumber * jvmNumber)
} }
@Benchmark
fun jvmMultiplyLarge(blackhole: Blackhole) = JBigIntegerField {
blackhole.consume(largeJvmNumber*largeJvmNumber)
}
@Benchmark
fun kmPower(blackhole: Blackhole) = BigIntField {
blackhole.consume(kmNumber.pow(bigExponent))
}
@Benchmark
fun jvmPower(blackhole: Blackhole) = JBigIntegerField {
blackhole.consume(jvmNumber.pow(bigExponent))
}
} }