On Tue, May 27, 2008 at 12:43 AM, Christian Heimes <lists@cheimes.de> wrote:
Ten minutes ago I raised a concern about speed differences between the old style % formatting and the new .format() code. Some quick benchmarking from Benjamin and me showed, that it's even worse than I expected.
My own tests show a less dire difference: $ ./python -m timeit "'some text with %s'.format('nothing')" 1000000 loops, best of 3: 0.578 usec per loop $ ./python -m timeit "'some text with %s' % 'nothing'" 1000000 loops, best of 3: 0.472 usec per loop
$ ./python -m timeit "'%s'.format('nothing')" 100000 loops, best of 3: 2.63 usec per loop $ ./python -m timeit "'%s' % 'nothing'" 10000000 loops, best of 3: 0.188 usec per loop
It struct me as odd that this one case shows such a big difference while the others show less of one. My tests show that the old-style % formatting is much faster when the final string is 20 characters or less: $ ./python -m timeit "'....|....|....|...%s' % '12'" 10000000 loops, best of 3: 0.0764 usec per loop $ ./python -m timeit "'....|....|....|...%s' % '123'" 1000000 loops, best of 3: 0.481 usec per loop A read through stringobject.c didn't give me any clue as to why 20 is the magic number. %d shows an even bigger jump: $ ./python -m timeit "'....|....|....|...%d' % 12" 10000000 loops, best of 3: 0.0764 usec per loop $ ./python -m timeit "'....|....|....|...%d' % 123" 1000000 loops, best of 3: 1.28 usec per loop Schiavo Simon