[Python-Dev] optimization required: .format() is much slower than %

Eric Smith eric+python-dev at trueblade.com
Tue May 27 17:13:57 CEST 2008


Eric Smith wrote:
> Nick Coghlan wrote:
>> Secondly, the string % operator appears to have an explicit 
>> optimisation for the 'just return str(self)' case. This optimisation 
>> is missing from the new string format method.
> 
> I'll see if I can optimize this case.

3.0, from svn:
$ ./python.exe -m timeit "'some text with {0}'.format('nothing')"
100000 loops, best of 3: 3.14 usec per loop

3.0, with PyUnicode_ExactCheck, skipping __format__ lookup:
$ ./python.exe -m timeit "'some text with {0}'.format('nothing')"
100000 loops, best of 3: 2.32 usec per loop

I could probably improve this some more, by skipping the creation of the 
object used to describe the format_spec.

Compare with:
$ ./python.exe -m timeit "'some text with %s' % 'nothing'"
1000000 loops, best of 3: 1.37 usec per loop


More information about the Python-Dev mailing list