Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
math/big: printing is non-linear and very slow for large Floats #11068
The Text method of big.Float produces a textual representation of the floating point number. I have a program that can create very large numbers and they are slow to print. Moreover, the time to print them does not scale sensibly with the size of the number:
% time ivy -e '1e10000' > /dev/null
I have investigated to some extent and the time is definitely being spent inside Text.
added a commit
Jun 23, 2015
Analysis: For large exponents, the decimal conversion creates the exact binary value in "full precision", which is then converted into a decimal string at decimal.go:75 by calling m.decimalString(), and then rounded to the desired number of decimal digits. For large positive exponents, most of the time is spent in this function (natconv.go:240) and eventually in nat.string (natconv.go:253).
Here's a small driver function to call the involved routines and expose the behaviour:
When the number of desired digits is small and exponents large, we should avoid this conversion path.
From e-mail thread:
here is an idea about how to do the floating point prints more
this code works but is missing many important properties, such as
Not addressed yet. I think it's a localized fix, but subtle to get right.
On Wed, Dec 9, 2015 at 7:43 AM, Brad Fitzpatrick email@example.com
for 1e50000 this takes 3 ms, for 1e500000 - 282 ms and for 1e5000000 - 37 seconds.
(I get the same times if I convert the big.Int to a big.Float and print that.)
@realityexists not really.
The problem with
But you'd expect the times to grow exponentially, right? The size in memory of a
That's because -as Robert wrote above- we go through decimal conversion with too many digits, and decimal conversion is slow. The proposed fix is to not do that.
I agree that
I don't know exactly how
Ah, I missed that - thanks for pointing it out. So perhaps