Steven D'Aprano <steve+pyt...@pearwood.info> added the comment: I think the difficulty here is that your perspective is backwards. It isn't that sorting a generator is *slower*, it is that sorting a list is *faster*, because there is more information available with a list and so the interpreter can take a short-cut.
With a generator or iterator, the interpreter doesn't know how many items will be in the finished collection, and so it has to build the list in stages as needed, growing it when it runs out of room, and possibly shrinking it if it grows too big. This takes time. But with a list or other sequence with a known length, the interpreter can allocate the right number of items up front, and avoid growing or shrinking the new list. I believe that this is the time saving you are seeing. So I don't think this is a bug, and I don't think there's any room to optimize the generator comprehension case. Unless somebody who knows more about the interpreter internals than I do speaks up to say there is a way to optimize this case, I think there's nothing that can be done. ---------- nosy: +steven.daprano _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue32945> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com