2009/12/12 Matias Graña <matias.alejo@???>:
> Hasta donde entiendo, armar el heap es N.log(N). Pero igual tomar los
> primeros n parece que está bien optimizado:
>
> $ python -m timeit 'import random,heapq;
> l=random.sample(xrange(100000000),1234567); m=sorted(l)[:10]'
> 10 loops, best of 3: 3.08 sec per loop
>
> $ python -m timeit 'import random,heapq;
> l=random.sample(xrange(100000000),1234567); m=heapq.nsmallest(10,l)'
> 10 loops, best of 3: 1.76 sec per loop
No metas en el loop del timing lo que no te interesa medir:
facundo@phenomenux:~$ timeit.py -s "import
random,heapq;l=random.sample(xrange(1000000),123456)"
"m=sorted(l)[:10]"
10 loops, best of 3: 137 msec per loop
facundo@phenomenux:~$ timeit.py -s "import
random,heapq;l=random.sample(xrange(1000000),123456)"
"m=heapq.nsmallest(10,l)"
10 loops, best of 3: 55.1 msec per loop
(los resultados indican lo mismo que antes, pero ahora tienen sentido
a nivel relativo entre ellos)
Slds.
--
. Facundo
Blog:
http://www.taniquetil.com.ar/plog/
PyAr:
http://www.python.org/ar/
---------------------------------------------------------------------
Para dar de baja la suscripcion, mande un mensaje a:
pyar-unsubscribe@???
Para obtener el resto de direcciones-comando, mande un mensaje a:
pyar-help@???
PyAr - Python Argentina - Sitio web:
http://www.python.com.ar/