This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients serhiy.storchaka
Date 2012-03-16.21:09:29
SpamBayes Score 1.9708192e-07
Marked as misclassified No
Message-id <1331932171.45.0.56771909125.issue14339@psf.upfronthosting.co.za>
In-reply-to
Content
This patch slightly optimize conversion int to string with base 2, 8 or 16 by calculating exactly number of digits and avoiding unnecessary memory allocation. The code is similar to used for decimal conversion.

Without patch:
$ ./python -m timeit -s 'x=1' 'bin(x)'
1000000 loops, best of 3: 0.295 usec per loop
$ ./python -m timeit -s 'x=1' 'oct(x)'
1000000 loops, best of 3: 0.327 usec per loop
$ ./python -m timeit -s 'x=1' 'hex(x)'
1000000 loops, best of 3: 0.298 usec per loop
$ ./python -m timeit -s 'x=9**999' 'bin(x)'
100000 loops, best of 3: 11.9 usec per loop
$ ./python -m timeit -s 'x=9**999' 'oct(x)'
100000 loops, best of 3: 4.55 usec per loop
$ ./python -m timeit -s 'x=9**999' 'hex(x)'
100000 loops, best of 3: 3.99 usec per loop

With patch:
$ ./python -m timeit -s 'x=1' 'bin(x)'
1000000 loops, best of 3: 0.213 usec per loop
$ ./python -m timeit -s 'x=1' 'oct(x)'
1000000 loops, best of 3: 0.228 usec per loop
$ ./python -m timeit -s 'x=1' 'hex(x)'
1000000 loops, best of 3: 0.215 usec per loop
$ ./python -m timeit -s 'x=9**999' 'bin(x)'
100000 loops, best of 3: 9.76 usec per loop
$ ./python -m timeit -s 'x=9**999' 'oct(x)'
100000 loops, best of 3: 3.58 usec per loop
$ ./python -m timeit -s 'x=9**999' 'hex(x)'
100000 loops, best of 3: 3.3 usec per loop
History
Date User Action Args
2012-03-16 21:09:31serhiy.storchakasetrecipients: + serhiy.storchaka
2012-03-16 21:09:31serhiy.storchakasetmessageid: <1331932171.45.0.56771909125.issue14339@psf.upfronthosting.co.za>
2012-03-16 21:09:30serhiy.storchakalinkissue14339 messages
2012-03-16 21:09:30serhiy.storchakacreate