spire icon indicating copy to clipboard operation
spire copied to clipboard

Faster parsing of hex and decimal represented big numbers

Open plokhotnyuk opened this issue 6 years ago • 0 comments

Default constructors of BigInt (and BigDecimal) from the string representation have O(n^2) complexity, while for power of 2 bases (like 16) and for others (like 10) corresponding complexities can be archived: O(n) and O(n^1.5).

scala> def timed[A](f: => A): A = { val t = System.currentTimeMillis; val r = f; println(s"Elapsed time: ${System.currentTimeMillis - t}"); r }
timed: [A](f: => A)A

scala> timed(BigInt("9" * 10000))
Elapsed time: 2

scala> timed(BigInt("9" * 100000))
Elapsed time: 131

scala> timed(BigInt("9" * 1000000))
Elapsed time: 13204

scala> timed(BigInt("F" * 10000, 16))
Elapsed time: 2

scala> timed(BigInt("F" * 100000, 16))
Elapsed time: 210

scala> timed(BigInt("F" * 1000000, 16))
Elapsed time: 20356

Does spire have more efficient constructors from string representations?

plokhotnyuk avatar Mar 18 '19 15:03 plokhotnyuk