[issue14687] Optimize str%tuple for the PEP 393

STINNER Victor report at bugs.python.org
Sat Apr 28 02:47:59 CEST 2012


New submission from STINNER Victor <victor.stinner at gmail.com>:

PyUnicode_Format() creates short temporary substrings. Attached patch tries to avoid substrings. For example, it avoids write of 1 character and repetition of 1 character like a space. PyUnicode_Format() now works in two steps:

 - computes the maximum character and the length of the output string
 - write characters into the output string

I'm not sure that my patch is correct, nor that the change does really speed-up Python.


Benchmark:

./python -m timeit \
  -s 's="x=%s, y=%u, z=%x"; args=(123, 456, 789)' \
  's%args'
./python -m timeit \
  -s 's="The %(k1)s is %(k2)s the %(k3)s."; args={"k1":"x","k2":"y","k3":"z",}' \
  's%args'

Python 3.2:

1000000 loops, best of 3: 0.482 usec per loop
1000000 loops, best of 3: 0.295 usec per loop

Python 3.3:

1000000 loops, best of 3: 0.653 usec per loop
1000000 loops, best of 3: 0.666 usec per loop

Python 3.3 + patch:

1000000 loops, best of 3: 0.596 usec per loop
1000000 loops, best of 3: 0.566 usec per loop

----------
files: pyunicode_format.patch
keywords: patch
messages: 159508
nosy: haypo, loewis, pitrou
priority: normal
severity: normal
status: open
title: Optimize str%tuple for the PEP 393
type: performance
versions: Python 3.3
Added file: http://bugs.python.org/file25388/pyunicode_format.patch

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


More information about the Python-bugs-list mailing list