efficient partial sort in Python ?

Terry Reedy tjreedy at udel.edu
Tue Aug 19 18:11:43 EDT 2014


On 8/19/2014 3:37 PM, Chiu Hsiang Hsu wrote:
> On Tuesday, August 19, 2014 5:42:27 AM UTC+8, Dan Stromberg wrote:
>> On Mon, Aug 18, 2014 at 10:18 AM, Chiu Hsiang Hsu
>> <wdv4758h at gmail.com> wrote:
>>
>>> I know that Python use Timsort as default sorting algorithm and
>>> it is efficient,

Especially with data that is partially ordered already.

>>> but I just wanna have a partial sorting (n-largest/smallest
>>> elements).

>> Perhaps heapq with Pypy?  Or with nuitka?  Or with numba?

> I heard of PyPy and numba before, but I doesn't know nuitka, thanks
> for your information.
>
> Another problem with heapq is the memory usage, it cost a lot of more
> memory with heapq in CPython (I test it in 3.4 with 1000000 float
> numbers) compare to sorted.
>
> For curiosity, there are many speed up solution in Python (like
> Cython, PyPy), I hasn't use Cython before, I guess PyPy is a more
> convient way to speed up current Python code (?), so how does Cython
> compare to PyPy ? (speed, code, flexibility, or anything else)

Why are you rejecting the previous answer: use .sort and pick out 
whatever items you want?


-- 
Terry Jan Reedy




More information about the Python-list mailing list