[Python-Dev] optimization required: .format() is much slower than %
Antoine Pitrou
solipsis at pitrou.net
Sun Jun 1 00:45:24 CEST 2008
Simon Cross <hodgestar <at> gmail.com> writes:
> My tests show that the old-style % formatting is much faster when the
> final string is 20 characters or less:
>
> $ ./python -m timeit "'....|....|....|...%s' % '12'"
> 10000000 loops, best of 3: 0.0764 usec per loop
You are the victim of a constant-folding optimization:
$ ./python -m timeit "'....|....|....|...%s' % '12'"
10000000 loops, best of 3: 0.0926 usec per loop
$ ./python -m timeit -s "s='12'" "'....|....|....|...%s' % s"
1000000 loops, best of 3: 0.525 usec per loop
>>> def f(): return '....|....|....|...%s' % '12'
...
>>> dis.dis(f)
1 0 LOAD_CONST 3 ('....|....|....|...12')
3 RETURN_VALUE
cheers
Antoine.
More information about the Python-Dev
mailing list