Pythonic way to iterate through multidimensional space?

Gayathri J usethisid2014 at gmail.com
Wed Aug 6 09:27:25 EDT 2014


Dear Peter

thanks . But thats what I was trying to say

just taking them to zero by f[:,:,:] = 0.0 or using np.zeros is surely
going to give me a time gain...
but my example of using the itertools.product() and doing f[x] =0.0 is just
to compare the looping timing with the traditional nested loops and not to
distract us to the operation done inside the loop.

right?


On Wed, Aug 6, 2014 at 6:09 PM, Peter Otten <__peter__ at web.de> wrote:

> Gayathri J wrote:
>
> > Dear Peter
> >
> > Yes the f[t] or f[:,:,:] might give  a marginal increase,
>
> The speedup compared itertools.product() is significant:
>
> $ python -m timeit -s 'from itertools import product; from numpy.random
> import rand; N = 100; a = rand(N, N, N); r = range(N)' 'for x in product(r,
> repeat=3): a[x] = 0.0'
> 10 loops, best of 3: 290 msec per loop
>
> $ python -m timeit -s 'from itertools import product; from numpy.random
> import rand; N = 100; a = rand(N, N, N); r = range(N)' 'a[:,:,:] = 0.0'
> 100 loops, best of 3: 3.58 msec per loop
>
> But normally you'd just make a new array with numpy.zeros().
>
> > but then i need
> > to do further operations  using the indices, in which case this wouldnt
> > help
>
> Which is expected and also the crux of such micro-benchmarks. They distract
> from big picture.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140806/f19f7efc/attachment.html>


More information about the Python-list mailing list