Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer to string conversion is extremely slow #6189

Closed
2 tasks done
ehsantn opened this issue Aug 29, 2020 · 1 comment · Fixed by #6202
Closed
2 tasks done

Integer to string conversion is extremely slow #6189

ehsantn opened this issue Aug 29, 2020 · 1 comment · Fixed by #6202
Labels
performance - run time Performance issue occurring at run time. performance performance related issue

Comments

@ehsantn
Copy link
Collaborator

ehsantn commented Aug 29, 2020

Reporting a bug

Integer to string conversion (str(int)) is extremely slow in Numba (~10x slower than Python and ~100x slower than C). The implementation (here) looks very inefficient since it allocates every single produced character. My guess is that someone copied it from CPython, but unlike Numba, CPython has string interning. The integer to string implementation can be much more efficient. Also, SSO (#3965) can avoid these allocations for many string programs in general.

In [7]: def f(n):
   ...:     c = 0
   ...:     for i in range(n):
   ...:         c += len(str(n))
   ...:     return c
   ...:

In [8]: %timeit f(1000_000)
194 ms ± 3.17 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [10]: g = numba.njit(f)

In [11]: g(1000_000)
Out[11]: 7000000

In [12]: %timeit g(1000_000)
1.97 s ± 68.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
@sklam
Copy link
Member

sklam commented Aug 31, 2020

I can confirm the 10x speed difference.

@stuartarchibald stuartarchibald added the performance - run time Performance issue occurring at run time. label Sep 1, 2020
stuartarchibald added a commit to stuartarchibald/numba that referenced this issue Sep 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance - run time Performance issue occurring at run time. performance performance related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants