Asymptotically fast conversion of a 'str' to an 'int'.
(s)
| 221 | return sign + s |
| 222 | |
| 223 | def _str_to_int_inner(s): |
| 224 | """Asymptotically fast conversion of a 'str' to an 'int'.""" |
| 225 | |
| 226 | # Function due to Bjorn Martinsson. See GH issue #90716 for details. |
| 227 | # https://github.com/python/cpython/issues/90716 |
| 228 | # |
| 229 | # The implementation in longobject.c of base conversion algorithms |
| 230 | # between power-of-2 and non-power-of-2 bases are quadratic time. |
| 231 | # This function implements a divide-and-conquer algorithm making use |
| 232 | # of Python's built in big int multiplication. Since Python uses the |
| 233 | # Karatsuba algorithm for multiplication, the time complexity |
| 234 | # of this function is O(len(s)**1.58). |
| 235 | |
| 236 | DIGLIM = 2048 |
| 237 | |
| 238 | def inner(a, b): |
| 239 | if b - a <= DIGLIM: |
| 240 | return int(s[a:b]) |
| 241 | mid = (a + b + 1) >> 1 |
| 242 | return (inner(mid, b) |
| 243 | + ((inner(a, mid) * w5pow[b - mid]) |
| 244 | << (b - mid))) |
| 245 | |
| 246 | w5pow = compute_powers(len(s), 5, DIGLIM) |
| 247 | return inner(0, len(s)) |
| 248 | |
| 249 | |
| 250 | # Asymptotically faster version, using the C decimal module. See |
nothing calls this directly
no test coverage detected
searching dependent graphs…