[Numpy-discussion] which one is best?

David M. Kaplan David.Kaplan@ird...
Fri Sep 19 09:24:48 CDT 2008


Hi Arnar,

Your two commands below aren't doing the same thing - one is doing
a[i]*b[i] and the other is doing a[i]*b[j] for all i and j.  As the
second is harder, it takes longer.

Cheers,
David

On Fri, 2008-09-19 at 09:08 -0500, numpy-discussion-request@scipy.org
wrote:
> I think
> [x*y for x in a for y in b]
> feels pythonic, however it has a surprisingly lousy performance.
> 
> In [30]: %timeit [ x * y for x,y in zip(a,b) ]
> 100000 loops, best of 3: 3.96 ?s per loop
> 
> In [31]: %timeit [ i*j for i in a for j in b ]
> 100000 loops, best of 3: 6.53 ?s per loop
> 
> In [32]: a = range(100)
> 
> In [33]: b = range(100)
> 
> In [34]: %timeit [ x * y for x,y in zip(a,b) ]
> 10000 loops, best of 3: 51.9 ?s per loop
> 
> In [35]: %timeit [ i*j for i in a for j in b ]
> 100 loops, best of 3: 2.78 ms per loop
> 
> Arnar
-- 
**********************************
David M. Kaplan
Charge de Recherche 1
Institut de Recherche pour le Developpement
Centre de Recherche Halieutique Mediterraneenne et Tropicale
av. Jean Monnet
B.P. 171
34203 Sete cedex
France

Phone: +33 (0)4 99 57 32 27
Fax: +33 (0)4 99 57 32 95
http://www.ur097.ird.fr/team/dkaplan/index.html
**********************************




More information about the Numpy-discussion mailing list