[issue32945] sorted(generator) is slower than sorted(list-comprehension)

Raymond Hettinger report at bugs.python.org
Sun Feb 25 12:30:35 EST 2018


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

[Antony Lee]
> so both cases are much more similar -- superficially, at least.

Try disassembling the inner code object as well -- that is where the work gets done and the list comprehension can take advantage of the LIST_APPEND opcode (rather than passing data to list_extend through an iterator which has more overhead).

[Stefan Behnel]
> The difference is that comprehensions are generally more efficient than generators, simply because they are more specialised.

Yes, that is most succinct description of why would expect a difference.

[Steven D'Aprano]
> So I don't think this is a bug, and I don't think there's any room to optimize the generator comprehension case.

I concur.

[Antony Lee]
> Feel free to close the issue if that's not the forum for this discussion, but I'm still baffled by what's happening.

Yes, this discussion is more suited to a StackOverflow entry where people commonly ask about why Python behaves as it does.  The bug tracker is more suited to known regressions or provable optimizations.  (One forum is for "I'm baffled" and the other is for "I have an improvement").

Marking this a closed.  If some demonstrable optimization is found, feel free to reopen.

----------
nosy: +rhettinger
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32945>
_______________________________________


More information about the Python-bugs-list mailing list