Motivation:
$ ./highest --help Usage: ./highest [-n numtokeep] [-f fieldno] [-a] [-r] [-h] [--filename fn] -n n specifies the number of numbers to keep -f n says to compare on the nth field (0 based) -a says to sort in ASCII order, rather than numerically -r says to sort in reverse -s sent says if a field cannot be converted to a float, to use sentinel value "sent" instead when sorting -h says to give usage information (help) --disallow-duplicates (or just -d) says to only keep one value of a given size --filename fn says to read from file fn --bufsize n says to read using a buffer size of n; only meaningful with --filename --use-bisect --use-treap --use-heap --use-rbtree --use-fibonacci-heap Reads from stdin by default, but that's considerably slower than --filename /dev/stdin
| Means | Running time | Storage required | Comments |
| GNU sort | tail | O(n*log2(n)+m) | O(n+m) | Fast for small lists. A remarkably good sort |
| highest with bisect.insort | O(n*m) | O(m) | This is pretty good. It outpaces heapq for a while, but then it suddenly skyrockets |
| highest with heapq | O(n*log2(m)) | O(m) | Pretty fast even for small lists, and doesn't require any nondefault modules. This is the default option. |
| highest with a treap | O(n*log2(m)) | O(m) | Not really that fast |
| highest with a fibonacci heap | O(n*log2(m)) | O(m) | Not really that fast |
| highest with a Red-Black tree | O(n*log2(m)) | O(m) | Not really that fast |
Here is a pair of graphs (in PDF format) describing how the various
datastructures perform.
As a quick reminder, we're looking at n numbers, and
keeping the m best values (lines):
graph-n.pdf
graph-m.pdf