mirror of
https://github.com/python/cpython
synced 2026-09-26 16:21:04 +03:00
For converting large ints to strings, CPython invokes a function in _pylong.py, which uses the decimal module to implement an asymptotically waaaaay sub-quadratic algorithm. But if the C decimal module isn't available, CPython uses _pydecimal.py instead. Which in turn frequently does str(int). If the int is very large, _pylong ends up doing the work, which in turn asks decimal to do "big" arithmetic, which in turn calls str(big_int), which in turn ... it can become infinite mutual recursion. This change introduces a different int->str function that doesn't use decimal. It's asymptotically worse, "Karatsuba time" instead of quadratic time, so still a huge improvement. _pylong switches to that when the C decimal isn't available. It is also used for not too large integers (less than 450_000 bits), where it is faster (up to 2 times for 30_000 bits) than the asymptotically better implementation that uses the C decimal. Co-authored-by: Tim Peters <tim.peters@gmail.com>
4 lines
235 B
ReStructuredText
4 lines
235 B
ReStructuredText
Break a loop between the Python implementation of the :mod:`decimal` module
|
|
and the Python code for integer to string conversion. Also optimize integer
|
|
to string conversion for values in the range from 9_000 to 135_000 decimal
|
|
digits.
|