Implement bignum module and write basic benchmark
This commit is contained in:
parent
bea2f2f46d
commit
3b52eb7ac1
@ -32,6 +32,8 @@ dependencies {
|
||||
implementation(project(":kmath-koma"))
|
||||
implementation(project(":kmath-viktor"))
|
||||
implementation(project(":kmath-dimensions"))
|
||||
implementation(project(":kmath-bignum"))
|
||||
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")
|
||||
|
@ -9,8 +9,9 @@ import scientifik.kmath.operations.RealField
|
||||
import kotlin.random.Random
|
||||
import kotlin.system.measureTimeMillis
|
||||
|
||||
class ExpressionsInterpretersBenchmark {
|
||||
private class ExpressionsInterpretersBenchmark {
|
||||
private val algebra: Field<Double> = RealField
|
||||
|
||||
fun functionalExpression() {
|
||||
val expr = algebra.expressionInField {
|
||||
variable("x") * const(2.0) + const(2.0) / variable("x") - const(16.0)
|
||||
|
@ -0,0 +1,61 @@
|
||||
package scientifik.kmath.bignum
|
||||
|
||||
import com.ionspin.kotlin.bignum.integer.BigInteger
|
||||
import scientifik.kmath.operations.BigIntField
|
||||
import scientifik.kmath.operations.invoke
|
||||
import kotlin.concurrent.thread
|
||||
import kotlin.random.Random
|
||||
import kotlin.system.measureTimeMillis
|
||||
|
||||
private class BigIntegerBenchmark {
|
||||
fun java() {
|
||||
invokeAndSum { l, l2 -> (l.toBigInteger() * l2.toBigInteger()).toLong() }
|
||||
}
|
||||
|
||||
fun bignum() {
|
||||
invokeAndSum { l, l2 -> (BigInteger.fromLong(l) * BigInteger.fromLong(l2)).longValue() }
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
thread {
|
||||
val long = measureTimeMillis(benchmark::long)
|
||||
println("long=$long")
|
||||
}
|
||||
}
|
10
kmath-bignum/build.gradle.kts
Normal file
10
kmath-bignum/build.gradle.kts
Normal file
@ -0,0 +1,10 @@
|
||||
plugins { id("scientifik.mpp") }
|
||||
|
||||
kotlin.sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api("com.ionspin.kotlin:bignum:0.2.0")
|
||||
api(project(":kmath-core"))
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package scientifik.kmath.bignum
|
||||
|
||||
import com.ionspin.kotlin.bignum.integer.BigInteger
|
||||
import scientifik.kmath.operations.Ring
|
||||
|
||||
class BigIntegerRing : Ring<BigInteger> {
|
||||
override val zero: BigInteger
|
||||
get() = BigInteger.ZERO
|
||||
|
||||
override val one: BigInteger
|
||||
get() = BigInteger.ONE
|
||||
|
||||
override fun number(value: Number): BigInteger = BigInteger.fromLong(value.toLong())
|
||||
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))
|
||||
override fun BigInteger.times(k: Number): BigInteger = multiply(number(k))
|
||||
}
|
@ -47,5 +47,6 @@ include(
|
||||
":kmath-for-real",
|
||||
":kmath-geometry",
|
||||
":kmath-ast",
|
||||
":kmath-bignum",
|
||||
":examples"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user