[SciPy-user] Re: vectorize(function)/arraymap did not return arrays?

Fernando Perez Fernando.Perez at colorado.edu
Fri Jan 21 16:13:00 EST 2005


Yichun Wei wrote:
> Well, I found list comprehension is actually fast enough.
> 
> res = [myconvolve(a[x,y], k[x,y]) for x in range(16) for y in range(16)]
> 
> which is much faster than
> 
> for x in range(16):
>      for y in range(16):
>          .....

You might want to preallocate those ranges() outside:

rng = range(NN)

res = [myconvolve(a[x,y], k[x,y]) for x in rng for y in rng]

For large NN, it will make a difference.  You have to remember that the python 
'compiler' is _extremely_ primitive, and it does not fold constants out of 
loops, no matter how trivial they may appear.  I can bet that this (with the 
many possible variations on the idea) is probably the single most damaging 
cause of 'slow python loops'.  By carefully doing certain things, you'd be 
surprised at how much you can get away with while still coding loops in python 
  (I won't say you can beat Fortran/C, but a few simple things can really go a 
long way).

Cheers,

f




More information about the SciPy-User mailing list