MCPcopy Index your code
hub / github.com/python/cpython / _str_to_int_inner

Function _str_to_int_inner

Lib/_pylong.py:223–247  ·  view source on GitHub ↗

Asymptotically fast conversion of a 'str' to an 'int'.

(s)

Source from the content-addressed store, hash-verified

221 return sign + s
222
223def _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

Callers

nothing calls this directly

Calls 2

compute_powersFunction · 0.85
innerFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…