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

Nick Coghlan ncoghlan at gmail.com
Thu May 29 15:44:01 CEST 2008


skip at pobox.com wrote:
>     Nick> $ ./python -m timeit "'' % ()"
>     Nick> 1000000 loops, best of 3: 0.389 usec per loop
> 
> vs.
> 
>     Nick> $ ./python -m timeit "'%s' % 'nothing'"
>     Nick> 10000000 loops, best of 3: 0.0736 usec per loop
> 
> I think you need to use a tuple for the second case to make it comparable to
> the first.

It doesn't actually make that big a difference - I'm guessing a 
Py_INCREF shortcut ends up getting used either way:

$ ./python -m timeit "'%s' % 'nothing'"
10000000 loops, best of 3: 0.0848 usec per loop
$ ./python -m timeit "'%s' % 'nothing',"
10000000 loops, best of 3: 0.133 usec per loop
$ ./python -m timeit "'' % ()"
1000000 loops, best of 3: 0.513 usec per loop

(times are a bit variable at this very moment since I have a few 
different apps open)

Cheers,
Nick.

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


More information about the Python-Dev mailing list