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

Eric Smith eric at trueblade.com
Mon Aug 18 16:39:35 CEST 2008

Eric Smith wrote:
> Eric Smith wrote:
>> 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
> r63826 has an optimization when formatting types that are exactly 
> unicode, long, or float (not subclasses).  Unfortunately it's only for 
> 3.0.  For 2.6 it's messier.  I'm still pondering what to do with that. 
> I'll get to it sometime, but maybe after the beta.

I finally backported this to 2.6 in r65814.  There's a similar 30% 
speedup for the simplest cases.  Unicode optimization is worse than 
string optimization, because of the way int, long, and float formatters 
work.  This can be fixed, but I'm not sure the gain justifies the 
effort.  I doubt I'll have time to look into this before beta3.


More information about the Python-Dev mailing list