Implement decimal benchmark, update field implementation

This commit is contained in:
Iaroslav Postovalov 2020-09-13 17:02:30 +07:00
parent 855bab258a
commit 9c3330cb6d
No known key found for this signature in database
GPG Key ID: 70D5F4DCB0972F1B
3 changed files with 54 additions and 7 deletions

View File

@ -0,0 +1,41 @@
package scientifik.kmath.bignum
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.State
import scientifik.kmath.operations.JBigDecimalField
import scientifik.kmath.operations.invoke
import kotlin.random.Random
@State(Scope.Benchmark)
class BigDecimalBenchmark {
final var times: Int = 10000
@Benchmark
fun java() {
val random = Random(0)
var sum = JBigDecimalField.zero
repeat(times) {
sum += JBigDecimalField {
number(random.nextDouble(-1000000000.0, 1000000000.0)) / random.nextDouble(-1000000.0, 1000000.0)
}
}
println("java:$sum")
}
@Benchmark
fun bignum() {
val random = Random(0)
var sum = BigDecimalField.zero
repeat(times) {
sum += BigDecimalField {
number(random.nextDouble(-1000000000.0, 1000000000.0)) / random.nextDouble(-1000000.0, 1000000.0)
}
}
println("bignum:$sum")
}
}

View File

@ -10,7 +10,7 @@ import kotlin.random.Random
@State(Scope.Benchmark) @State(Scope.Benchmark)
class BigIntegerBenchmark { class BigIntegerBenchmark {
var times: Int = 1000000 final var times: Int = 1000000
@Benchmark @Benchmark
fun java() { fun java() {

View File

@ -2,9 +2,10 @@ package scientifik.kmath.bignum
import com.ionspin.kotlin.bignum.decimal.BigDecimal import com.ionspin.kotlin.bignum.decimal.BigDecimal
import com.ionspin.kotlin.bignum.decimal.DecimalMode import com.ionspin.kotlin.bignum.decimal.DecimalMode
import com.ionspin.kotlin.bignum.decimal.RoundingMode
import scientifik.kmath.operations.Field import scientifik.kmath.operations.Field
abstract class BigDecimalFieldBase internal constructor(val mode: DecimalMode = DecimalMode.DEFAULT) : abstract class BigDecimalFieldBase internal constructor(val mode: DecimalMode = DEFAULT_MODE) :
Field<BigDecimal> { Field<BigDecimal> {
override val zero: BigDecimal override val zero: BigDecimal
get() = BigDecimal.ZERO get() = BigDecimal.ZERO
@ -17,12 +18,17 @@ abstract class BigDecimalFieldBase internal constructor(val mode: DecimalMode =
override fun multiply(a: BigDecimal, k: Number): BigDecimal = a.times(number(k)) override fun multiply(a: BigDecimal, k: Number): BigDecimal = a.times(number(k))
override fun multiply(a: BigDecimal, b: BigDecimal): BigDecimal = a.times(b) override fun multiply(a: BigDecimal, b: BigDecimal): BigDecimal = a.times(b)
override fun divide(a: BigDecimal, b: BigDecimal): BigDecimal = a.divide(b) override fun divide(a: BigDecimal, b: BigDecimal): BigDecimal = a.divide(b)
override fun BigDecimal.times(k: Number): BigDecimal = times(number(k)) override fun BigDecimal.minus(b: BigDecimal): BigDecimal = subtract(b, mode)
override fun BigDecimal.plus(b: Number): BigDecimal = plus(number(b)) override fun BigDecimal.times(k: Number): BigDecimal = multiply(number(k), mode)
override fun BigDecimal.minus(b: Number): BigDecimal = minus(number(b)) override fun BigDecimal.plus(b: Number): BigDecimal = add(number(b), mode)
override fun BigDecimal.div(k: Number): BigDecimal = div(number(k)) override fun BigDecimal.minus(b: Number): BigDecimal = subtract(number(b), mode)
override fun BigDecimal.div(k: Number): BigDecimal = divide(number(k), mode)
companion object {
internal val DEFAULT_MODE = DecimalMode(16, RoundingMode.ROUND_HALF_AWAY_FROM_ZERO)
}
} }
class BigDecimalField(mode: DecimalMode = DecimalMode.DEFAULT) : BigDecimalFieldBase(mode) { class BigDecimalField(mode: DecimalMode = DEFAULT_MODE) : BigDecimalFieldBase(mode) {
companion object : BigDecimalFieldBase() companion object : BigDecimalFieldBase()
} }