I am writing a program where time is important, and I just realized through a lot of debugging prints that my big holdup (80% of computing time) is converting a very large BigInteger (50K digits) into a string.
Is this behavior to be expected or how can I change something to make it run faster?
I am writing a program where time is important, and I just realized through
Share
Converting numbers to strings is an expensive operation even if you use
longanddouble.Normally, the only thing more expensive is the IO you perform when writing the text for a file or the console.
It is worth noting that the built in converter a number to text is an O(N^2) operation where N is the number of digits. As such it is not surprising that 50K digit numbers take a very long time to convert to a decimal String.
Based on tmyklebu’s suggestion I have written this. It is slower for numbers with less than 500 digits, but is much faster in the range of 50,000 digits.
prints