3 ms·
TFA explains this, although they never actually links the StackOverflow answer that they quote from. > CPython’s list.sort is implemented in C (avoiding interp
by nightpool 5y ago
TFA explains this, although they never actually links the StackOverflow answer that they quote from.
> CPython’s list.sort is implemented in C (avoiding interpreter overhead), while heapq.merge is mostly implemented in Python, and optimizes for the “many iterables” case in a way that slows the “two iterables” case.
Going to the original StackOverflow answer, we can see the same user (ShadowRanger) expanding on this in a comment: https://stackoverflow.com/questions/464342/combining-two-sorted-lists-in-python https://stackoverflow.com/questions/464342/combining-two-sor...
> The selling point for heapq.merge is that it doesn't require either the inputs or the outputs to be list; it can consume iterators/generators and produces a generator, so huge inputs/outputs (not stored in RAM at once) can be combined without swap thrashing. It also handles merging an arbitrary number of input iterables with lower overhead than might be expected (it uses a heap to coordinate the merging, so the overhead scales with the log of the number of iterables, not linearly, but as noted, that doesn't matter for the "two iterable" case).
- CogitoCogito 5y agoYeah looking at the all the features of the heapq.merge() version, I kind of doubt implementing it in C would speed it up much at all. edit: Yeah I now see where the author mentioned the design philosophy of the python version. It was a bit burried, but it answers my question like you say. I personally find that to be the most interesting part.