[issue32945] sorted(generator) is slower than sorted(list-comprehension)
Stefan Behnel
report at bugs.python.org
Sun Feb 25 04:54:32 EST 2018
Stefan Behnel <stefan_ml at behnel.de> added the comment:
The constant function call overhead doesn't make a big difference:
$ /opt/python3.7-opt/bin/python3 -m timeit 'list(i for i in range(1000))'
5000 loops, best of 5: 55 usec per loop
$ /opt/python3.7-opt/bin/python3 -m timeit '[i for i in range(1000)]'
10000 loops, best of 5: 30.7 usec per loop
The difference is that comprehensions are generally more efficient than generators, simply because they are more specialised. When a generator is created, it does not know whether it will be passed into list() to quickly unpack it into a list, or into some complex machinery that just requests one value per year, or only one value at all and then throws it away.
I searched a bit, but couldn't find a ticket about the performance difference above, although I'm sure there must be one. So I'll leave this open for now, assuming that there might still be something to improve here.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32945>
_______________________________________
More information about the Python-bugs-list
mailing list