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

Nick Coghlan ncoghlan at gmail.com
Fri May 30 11:47:35 CEST 2008


Simon Cross wrote:
> It struct me as odd that this one case shows such a big difference
> while the others show less of one.

I believe the %-formatting code has some optimisations in place where it 
realises it can just increment the reference count of the passed in 
string and return that, rather than having to build a new string object.

As for why you didn't see any differences in a couple of your tests: the 
.format() call wasn't actually substituting anything.

Unfortunately, the reasons why .format() doesn't complain about extra 
arguments never made it into the PEP. They can be found here:
http://mail.python.org/pipermail/python-dev/2006-May/065062.html

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list