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.

classification
Title: Use unicode_writer API for str.format()
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, kristjan.jonsson, loewis, pitrou, python-dev, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2012-05-03 22:08 by vstinner, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
unicode_format_writer.patch vstinner, 2012-05-03 22:08 review
Messages (4)
msg159879 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-05-03 22:08
I just added a new "unicode_writer" API for the issue #14687 (Optimize str%tuple for the PEP 393) which helps to make "str%tuple" between 25% and 30% faster.

Attached patch replaces PyAccu API with the unicode_writer API for str.format().

Python 3.2:

1000000 loops, best of 3: 0.198 usec per loop
100000 loops, best of 3: 11.3 usec per loop
10000000 loops, best of 3: 0.167 usec per loop
1000000 loops, best of 3: 0.494 usec per loop

Python 3.3:

1000000 loops, best of 3: 0.293 usec per loop
10000 loops, best of 3: 20.2 usec per loop
1000000 loops, best of 3: 0.219 usec per loop
1000000 loops, best of 3: 0.909 usec per loop

Python 3.3 + patch (speed up of the patch):

1000000 loops, best of 3: 0.226 usec per loop (-22%)
100000 loops, best of 3: 14.8 usec per loop (-26%)
1000000 loops, best of 3: 0.219 usec per loop (0%)
1000000 loops, best of 3: 0.658 usec per loop (-27%)
msg159880 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2012-05-03 22:11
Oh, I forgot the benchmark script:
------------
$ cat ~/bench_format.sh 
./python -m timeit \
    -s 'fmt="{}:"; arg="abc"' \
    'fmt.format(arg)'
./python -m timeit \
    -s 'N=200; L=3; fmt="{}"*N; args=("a"*L,)*N' \
    'fmt.format(*args)'
./python -m timeit \
    -s 's="x=%s, y=%u, z=%x"; args=(123, 456, 789)' \
    's.format(*args)'
./python -m timeit \
    -s 's="The {k1} is {k2} the {k3}."; args={"k1": "x", "k2": "y", "k3": "z"}' \
    's.format(**args)'
------------
(based on the one used for #14687, see msg159822)
msg160136 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-05-07 11:08
New changeset 7be716a47e9d by Victor Stinner in branch 'default':
Close #14716: str.format() now uses the new "unicode writer" API instead of the
http://hg.python.org/cpython/rev/7be716a47e9d

New changeset ab500b297900 by Victor Stinner in branch 'default':
Issue #14716: Change integer overflow check in unicode_writer_prepare()
http://hg.python.org/cpython/rev/ab500b297900
msg160175 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-05-07 21:50
New changeset 01581e8b50f2 by Victor Stinner in branch 'default':
Backout ab500b297900: the check for integer overflow is wrong
http://hg.python.org/cpython/rev/01581e8b50f2
History
Date User Action Args
2022-04-11 14:57:29adminsetgithub: 58921
2012-05-07 21:50:13python-devsetmessages: + msg160175
2012-05-07 11:08:10python-devsetstatus: open -> closed
resolution: fixed
messages: + msg160136

stage: resolved
2012-05-03 22:35:02eric.smithsetnosy: + eric.smith
2012-05-03 22:11:49vstinnersetmessages: + msg159880
2012-05-03 22:08:16vstinnercreate