Selecting k smallest or largest elements from a large list in python; (benchmarking)

Peter Otten __peter__ at web.de
Thu Sep 2 04:17:30 EDT 2010


Arnaud Delobelle wrote:

> I get:
> 
> 1.46s for _heapq.nsmallest
> 0.85s for nsmallest_slott_bisect2 (version I posted)
> 
> I am a bit surprised that mine is so slow compared with yours.  I'll
> do more tests later!

Strange. I see a significant difference only for python3 (on 64bit Linux)

$ python3 nsmallest_perf3.py
3.1.1+ (r311:74480, Nov  2 2009, 15:45:00)
[GCC 4.4.1]
pick the 10 smallest items out of 5000000
nsmallest               --> 0.310983181
nsmallest_slott_bisect  --> 1.02625894547
nsmallest_slott_bisect2 --> 0.612951040268

$ python nsmallest_perf3.py
2.6.4 (r264:75706, Dec  7 2009, 18:43:55)
[GCC 4.4.1]
pick the 10 smallest items out of 5000000
nsmallest               --> 0.743387937546
nsmallest_slott_bisect  --> 0.961116075516
nsmallest_slott_bisect2 --> 0.746085882187

Peter



More information about the Python-list mailing list