[Python-Dev] transitioning from % to {} formatting

Antoine Pitrou solipsis at pitrou.net
Fri Oct 2 21:08:21 CEST 2009


Raymond Hettinger <python <at> rcn.com> writes:
> 
> Because there has been limited uptake on {}-formatting (afaict),
> we still have limited experience with knowing that it is actually
> better, less error-prone, easier to learn/rember, etc.

It is known to be quite slower.

The following timings are on the py3k branch:

- with positional arguments:
$ ./python -m timeit -s "s='%s %s'; t = ('hello', 'world')" "s % t"
1000000 loops, best of 3: 0.313 usec per loop
$ ./python -m timeit -s "f='{} {}'.format; t = ('hello', 'world')" "f(*t)"
1000000 loops, best of 3: 0.572 usec per loop

- with named arguments:
$ ./python -m timeit -s "s='%(a)s %(b)s'; d = dict(a='hello', b='world')" "s % d"
1000000 loops, best of 3: 0.387 usec per loop
$ ./python -m timeit -s "f='{a} {b}'.format; d = dict(a='hello', b='world')"
"f(**d)"
1000000 loops, best of 3: 0.581 usec per loop


Regards

Antoine.




More information about the Python-Dev mailing list