4 ms·
A few months ago I wrote a C implementation of a lazy "sorted" function, (https://github.com/naftaliharris/lazysort https://github.com/naftaliharris/lazysort).
by naftaliharris 13y ago
A few months ago I wrote a C implementation of a lazy "sorted" function, (https://github.com/naftaliharris/lazysort https://github.com/naftaliharris/lazysort). One of the things you can use it for is computing medians, with the same "sort-then-take-the-middle-element" algorithm that the statistics module uses. Internally, however, what will happen is that the lazy data-structure that results from a call to the lazy "sorted" function will find the middle element with quickselect.
For lists of size 10M, I got a 7.3x speedup vs using the sorted function, and a 1.6x speedup vs using numpy.median. The sorted function was faster up until lists of size 100, presumably due to lower overhead. (The lazily-sorted list data-structure also keeps track of the pivots resulting from partitions, so that later method calls run faster by exploiting the partially-sorted structure that remains from earlier method calls).
Here's a plot of the time to compute the median for these different algorithms, (taken from a paper I wrote that's under review): http://imgur.com/oX1QLnS http://imgur.com/oX1QLnS