Skip to content

Commit

Permalink
Speed up str(int)
Browse files Browse the repository at this point in the history
As title.

Fixes numba#6189
  • Loading branch information
stuartarchibald committed Sep 1, 2020
1 parent d58a63a commit 3f8ac3e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
20 changes: 14 additions & 6 deletions numba/cpython/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2352,6 +2352,7 @@ def impl(i):
@overload(str)
def integer_str(n):
if isinstance(n, types.Integer):

ten = n(10)

def impl(n):
Expand All @@ -2361,16 +2362,23 @@ def impl(n):
flag = True
if n == 0:
return '0'
l = []
length = flag + 1 + int(np.floor(np.log10(n)))
kind = PY_UNICODE_1BYTE_KIND
char_width = _kind_to_byte_width(kind)
s = _malloc_string(kind, char_width, length, True)
const = ord('0')
if flag:
_set_code_point(s, 0, ord('-'))
idx = length - 1
while n > 0:
c = chr(ord('0') + (n % ten))
c = const + (n % ten)
n = n // ten
l.append(c)
if flag:
l.append('-')
return ''.join(l[::-1])
_set_code_point(s, idx, c)
idx -= 1
return s
return impl


# ------------------------------------------------------------------------------
# iteration
# ------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions numba/tests/test_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,14 @@ def test_int_str(self, flags=nrt_no_pyobj_flags):
1234,
1,
0,
10,
1000,
]

large_inputs = [
123456789,
2222222,
1000000,
~0x0
]

Expand Down

0 comments on commit 3f8ac3e

Please sign in to comment.