On Mon, Jun 6, 2011 at 6:57 AM, gary ruben <[email protected]> wrote:
> I learn a lot by watching the numpy and scipy lists (today Olivier
> taught me about heapq :), but he may not have noticed that Python 2.4
> added an nsmallest method)
>
> import heapq
> q = list(x)
> heapq.heapify(q)
> k_smallest = heapq.nsmallest(k,q)

I learned something new too---heapq.

Timings:

    >> alist = a.tolist()
    >> heapq.heapify(alist)
    >> timeit k_smallest = heapq.nsmallest(k, alist)
    100 loops, best of 3: 1.84 ms per loop
    >> timeit bn.partsort(a, a.size-10)
    10000 loops, best of 3: 39.3 us per loop
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to