Zhelenskiy
05/06/2021, 4:45 PMLongBased
type, it became 10 faster than generic one. The speed was the same with just manual long checking. This is because I used inline types. However, it is still a lot slower than just pure usage of Int
s and `Long`s. But that may be achieved if the code is run under HotSpot and it decides to use intrinsic there. Here is the repo: https://github.com/zhelenskiy/BigInteger/tree/main.Zhelenskiy
05/06/2021, 4:48 PMIaroslav Postovalov
05/06/2021, 5:21 PMZhelenskiy
05/06/2021, 5:29 PMIaroslav Postovalov
05/06/2021, 5:36 PMlong
and stores it as long
.
By extending sealed interface
idea you can implement arithmetic functions as extensions to
sealed interface BigInteger
which are matching the number to LongBasedBigInteger
or something else; however, there may be some issues with comparing the number and detecting overflows.Iaroslav Postovalov
05/06/2021, 5:40 PMlong
type is much faster than JDK BigInteger
.altavir
05/06/2021, 6:36 PMZhelenskiy
05/06/2021, 6:44 PMaltavir
05/06/2021, 6:45 PMZhelenskiy
05/06/2021, 6:47 PMaltavir
05/06/2021, 6:48 PMZhelenskiy
05/06/2021, 6:49 PMaltavir
05/06/2021, 6:49 PMZhelenskiy
05/06/2021, 6:51 PMZhelenskiy
05/06/2021, 6:51 PMaltavir
05/06/2021, 6:52 PMZhelenskiy
05/06/2021, 6:55 PMZhelenskiy
05/06/2021, 10:28 PMIntArray
of size 1, but if I use the 0-th element as UInt, I have no overflow, so next Int will be 0, trailing zero.Zhelenskiy
05/06/2021, 11:10 PMInt.MAX_VALUE
. Then we can store the flag with with capacity using bitmask.Zhelenskiy
05/09/2021, 9:49 PMLongBased
optimization because direct calls to the unoptimized code has the same low speed. However, it may be connected with that I do something wrong. I haven't published the version yet.
Small numbers
java.lang.Math.BigInteger: 2.18s 2.04s 1.84s 2.67s 1.86s
Kotlin (Generic Long based): 1.89s 1.41s 1.44s 1.81s 1.69s
Kotlin (Explicit Long based): 63.1ms 67.4ms 48.8ms 55.1ms 51.3ms
Kotlin (Generic Array based): 1.34s 1.52s 2.08s 1.59s 1.42s
Kotlin (Explicit Array based): 6.41s 5.98s 7.10s 6.58s 6.32s
Pure Int: 2.57ms 2.32ms 596us 7.31us 8.70us
Pure Long: 6.65ms 8.52ms 5.57ms 4.58ms 4.28ms
Checked Long: 37.3ms 33.4ms 85.0ms 77.7ms 72.1ms
For big numbers (kotlin array = direct call to array based):
Kotlin: 14.7ms Kotlin Array: 4.49ms Java: 9.92ms
Kotlin: 23.1ms Kotlin Array: 5.02ms Java: 3.20ms
Kotlin: 1.87ms Kotlin Array: 1.92ms Java: 582us
Kotlin: 2.17ms Kotlin Array: 2.06ms Java: 544us
Kotlin: 5.56ms Kotlin Array: 5.46ms Java: 731us
Kotlin: 3.51ms Kotlin Array: 3.28ms Java: 778us
Kotlin: 2.88ms Kotlin Array: 3.75ms Java: 188us
Kotlin: 2.99ms Kotlin Array: 2.73ms Java: 416us
Kotlin: 5.40ms Kotlin Array: 2.90ms Java: 341us
Kotlin: 1.60ms Kotlin Array: 1.55ms Java: 340us
Zhelenskiy
05/09/2021, 9:50 PMaltavir
05/10/2021, 6:16 AMZhelenskiy
05/10/2021, 1:09 PMZhelenskiy
05/10/2021, 3:16 PMaltavir
05/10/2021, 3:31 PMZhelenskiy
05/10/2021, 3:33 PMZhelenskiy
05/15/2021, 9:45 AMZhelenskiy
05/15/2021, 9:47 AMZhelenskiy
05/15/2021, 9:49 AMaltavir
05/15/2021, 9:57 AMZhelenskiy
05/15/2021, 9:59 AMtype alias Magnitude = UIntArray
with
@JvmInline
value class UBigInt(private val array: UIntArray) {
private inline operator fun get(index: Int) = array[index]
private inline operator fun set(index: Int, UInt) = ....
}Zhelenskiy
05/15/2021, 9:59 AMaltavir
05/15/2021, 10:19 AMZhelenskiy
05/15/2021, 10:36 AMZhelenskiy
05/16/2021, 9:34 PMZhelenskiy
05/16/2021, 9:39 PMZhelenskiy
05/28/2021, 7:38 AMaltavir
05/28/2021, 7:46 AMZhelenskiy
05/28/2021, 8:26 AMaltavir
05/28/2021, 8:27 AMZhelenskiy
05/28/2021, 8:28 AMaltavir
05/28/2021, 8:28 AMZhelenskiy
05/28/2021, 8:29 AMZhelenskiy
05/28/2021, 8:29 AMaltavir
05/28/2021, 8:30 AMaltavir
05/28/2021, 8:33 AMequals
approch was proven as inconsistent for now. Partially because of inlines.Zhelenskiy
05/28/2021, 8:38 AMaltavir
05/28/2021, 8:39 AMZhelenskiy
05/28/2021, 8:39 AMaltavir
05/28/2021, 8:40 AMZhelenskiy
05/28/2021, 8:45 AMaltavir
05/28/2021, 8:45 AMaltavir
05/28/2021, 8:48 AMaltavir
05/28/2021, 8:48 AMZhelenskiy
05/28/2021, 8:51 AMFalse
altavir
05/28/2021, 8:52 AMaltavir
05/28/2021, 8:52 AMZhelenskiy
05/28/2021, 9:00 AMaltavir
05/28/2021, 9:01 AMZhelenskiy
05/28/2021, 9:01 AMaltavir
05/28/2021, 9:04 AMaltavir
05/28/2021, 9:04 AMZhelenskiy
05/28/2021, 9:04 AMaltavir
05/28/2021, 9:05 AMZhelenskiy
05/28/2021, 9:09 AMaltavir
05/28/2021, 9:18 AMZhelenskiy
05/28/2021, 9:19 AMaltavir
05/28/2021, 9:20 AMZhelenskiy
05/28/2021, 9:38 AMZhelenskiy
05/28/2021, 9:44 AMZhelenskiy
05/28/2021, 9:47 AMaltavir
05/28/2021, 9:55 AMZhelenskiy
05/28/2021, 9:57 AMZhelenskiy
05/28/2021, 9:58 AMaltavir
05/28/2021, 10:09 AMZhelenskiy
05/28/2021, 11:07 AMaltavir
05/28/2021, 11:34 AMZhelenskiy
05/28/2021, 11:39 AM