Issue 14716: Use unicode_writer API for str.format() (original) (raw)

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%)

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 )