efficient partial sort in Python ?

Chiu Hsiang Hsu wdv4758h at gmail.com
Tue Aug 19 15:37:12 EDT 2014


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,
> 
> > 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)



More information about the Python-list mailing list