[Guido] > Can anybody explain this? > > [guido at guido linux]$ ./python ../Lib/timeit.py -s 'r=range(10000)' 'sum([x for x in r])' > 100 loops, best of 3: 7.75 msec per loop > [guido at guido linux]$ ./python ../Lib/timeit.py -s 'r=range(10000)' 'sum(x for x in r)' > 100 loops, best of 3: 8.23 msec per loop > > (I believe this is with the penultimate version of the patch from SF.) Does a sub-10% difference really need explanation? Resuming a generator function is a lot cheaper than calling a function, but there's still a non-trivial amount of code to get in and out of eval_frame() each time, which the listcomp version gets to skip. Make r a lot bigger, and I expect the genexp will get relatively faster (due to better cache behavior). Timing plain 'sum(r)' would also be revealing (of something <wink>).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4