[issue16001] small ints: cache string representation

STINNER Victor report at bugs.python.org
Tue Sep 25 01:25:21 CEST 2012


STINNER Victor added the comment:

Here is a micro benchmark:
---
# run it using:
# benchmark.py script bench_int_str.py [--file=output]
# https://bitbucket.org/haypo/misc/src/tip/python/benchmark.py

def run_benchmark(bench):
    bench.timeit('S(123)', setup='S=str')
    bench.timeit('S(1) == S(2)', setup='S=str')
    bench.timeit('S(12345)', setup='S=str')
    bench.timeit('{S(x): x for x in data}', setup='data=tuple(range(100)); S=str')
    bench.timeit('"x=%s" % x', setup='x=123')
    bench.timeit('"x=%s" % x', setup='x=12345')
---

Output:
-------------------------------------------------------+-------------+---------------
Tests                                                  |   unpatched |        patched
-------------------------------------------------------+-------------+---------------
S=str; S(123)                                          |  158 ns (*) |  112 ns (-29%)
S=str; S(1) == S(2)                                    |  329 ns (*) |  248 ns (-25%)
S=str; S(12345)                                        |  161 ns (*) |         161 ns
data=tuple(range(100)); S=str; {S(x): x for x in data} |   23 us (*) | 16.5 us (-28%)
x=123; "x=%s" % x                                      |  145 ns (*) |   133 ns (-8%)
x=12345; "x=%s" % x                                    |  149 ns (*) |         145 ns
-------------------------------------------------------+-------------+---------------
Total                                                  | 23.9 us (*) | 17.3 us (-27%)
-------------------------------------------------------+-------------+---------------

I expected more important speedup.

----------
Added file: http://bugs.python.org/file27285/bench_int_str.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16001>
_______________________________________


More information about the Python-bugs-list mailing list