Simon Cross <hodgestar <at> gmail.com> writes:
> 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
You are the victim of a constant-folding optimization:
$ ./python -m timeit "'....|....|....|...%s' % '12'"
10000000 loops, best of 3: 0.0926 usec per loop
$ ./python -m timeit -s "s='12'" "'....|....|....|...%s' % s"
1000000 loops, best of 3: 0.525 usec per loop
>>> def f(): return '....|....|....|...%s' % '12'
...
>>> dis.dis(f)
1 0 LOAD_CONST 3 ('....|....|....|...12')
3 RETURN_VALUE
cheers
Antoine.
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com