There is indeed a solid difference, but yeah, it's tiny. $ python --version Python 2.7.1
$ python -m timeit -r16 -s 'd=["a"*i for i in range(10000)]; l=len' -- 'for x in d:' ' if l(x) > 5000: x[:5000]' 100 loops, best of 16: 4.94 msec per loop
$ python -m timeit -r16 -s 'd=["a"*i for i in range(10000)]' -- 'for x in d:' ' x[:5000]' 100 loops, best of 16: 4.14 msec per loop
The numbers were pretty stable (around +-.4 ms, so +- 40ns per actual call at 10000 calls per loop) for several runs. These were the two fastest measurements for each approach. Not worth worrying about because if you've got an inner loop that tight, you need to either switch to pypy or write it in C.
Overhead: $ python -m timeit -r16 -s 'd=["a"*i for i in range(10000)]; l=len' -- 'for x in d: pass' 1000 loops, best of 16: 599 usec per loop
Comment
There is indeed a solid difference, but yeah, it's tiny.
$ python --version
Python 2.7.1
$ python -m timeit -r16 -s 'd=["a"*i for i in range(10000)]; l=len' -- 'for x in d:' ' if l(x) > 5000: x[:5000]'
100 loops, best of 16: 4.94 msec per loop
$ python -m timeit -r16 -s 'd=["a"*i for i in range(10000)]' -- 'for x in d:' ' x[:5000]'
100 loops, best of 16: 4.14 msec per loop
The numbers were pretty stable (around +-.4 ms, so +- 40ns per actual call at 10000 calls per loop) for several runs. These were the two fastest measurements for each approach. Not worth worrying about because if you've got an inner loop that tight, you need to either switch to pypy or write it in C.
Overhead:
$ python -m timeit -r16 -s 'd=["a"*i for i in range(10000)]; l=len' -- 'for x in d: pass'
1000 loops, best of 16: 599 usec per loop