percent faster than format()?

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Tue Apr 23 10:57:58 EDT 2013


Am 23.04.2013 10:26, schrieb Chris “Kwpolska” Warrick:
> On Tue, Apr 23, 2013 at 9:46 AM, Ulrich Eckhardt
> <ulrich.eckhardt at dominolaser.com> wrote:
>> Am 23.04.2013 06:00, schrieb Steven D'Aprano:
>>>
>>> If it comes down to micro-optimizations to shave a few microseconds off,
>>> consider using string % formatting rather than the format method.
>>
>>
>> Why? I don't see any obvious difference between the two...
[...]
>
> $ python -m timeit "a = '{0} {1} {2}'.format(1, 2, 42)"
> 1000000 loops, best of 3: 0.824 usec per loop
> $ python -m timeit "a = '%s %s %s' % (1, 2, 42)"
> 10000000 loops, best of 3: 0.0286 usec per loop
>

Well, I don't question that for at least some CPython implementations 
one is faster than the other. I don't see a reason why one must be 
faster than the other though. In other words, I don't understand where 
the other one needs more time to achieve basically the same. To me, the 
only difference is the syntax, but not greatly so.

So again, why is one faster than the other? What am I missing?

Uli




More information about the Python-list mailing list