ionspin / kotlin-multiplatform-bignum

A Kotlin multiplatform library for arbitrary precision arithmetics
Apache License 2.0
339 stars 40 forks source link

Improve performances of BigInteger.numberOfDecimalDigits(). #300

Open glureau opened 1 month ago

glureau commented 1 month ago

Similar PR than previous one but with focus on the BigInteger.

    @Test
    fun perfTest() {
        val bd = BigInteger.parseString("123")
        val duration = measureTime {
            repeat(1_000_000) { bd.numberOfDecimalDigits() }
        }
        println("Duration: $duration")
    }

Result (best of 3 runs)

platform Before After Time reduction
iosSimulator 4.60s 0.62s reduction of 86%
JVM 219ms 75.2ms reduction of 66%
macosArm64 4.91s 0.67ms reduction of 86%

All benchmark data

All runs are done on the same MacBook Pro M3 with 36GB RAM.

Before

After


Just a side note, I took a relatively short value for the benchmark, I guess performances may vary with length (like the previous implementation). I only run one more test on JVM with "123456789012345678901234567890123456", before 232ms, after 54ms, so I guess we're still in a better place.