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

Steve Holden steve at holdenweb.com
Thu May 29 20:18:18 CEST 2008


Nick Coghlan wrote:
> 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

Technically there you aren't using a tuple to provide the substitution 
argument, you are doing the substitution from a single string then 
putting the result in a tuple:

 >>> '%s' % 'nothing',
('nothing',)
 >>>

On the basis of evidence like this:

sholden at lifeboy ~
$ python -m timeit "'%s' % 'nothing'"
10000000 loops, best of 3: 0.0705 usec per loop

sholden at lifeboy ~
$ python -m timeit "'%s' % ('nothing',)"
1000000 loops, best of 3: 0.691 usec per loop

I'd say not using a tuple was the way to go if ever you needed to 
optimize the code for speed.

> $ ./python -m timeit "'' % ()"
> 1000000 loops, best of 3: 0.513 usec per loop
> 
If you want an even stranger result, it appears to take the interpreter 
longer to substitute nothing from a tuple than it does to substitute the 
empty string from a string:

sholden at lifeboy ~
$ python -m timeit "'' % ()"
1000000 loops, best of 3: 0.454 usec per loop

sholden at lifeboy ~
$ python -m timeit "'%s' % ''"
10000000 loops, best of 3: 0.0715 usec per loop

> (times are a bit variable at this very moment since I have a few 
> different apps open)
> 
Me too. Timings are not benchmarks, I am not a lawyer, etc. Our machines 
seem to be of comparable speed.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/



More information about the Python-Dev mailing list