On Jul 19, 2011, at 3:15 PM, Chad Netzer wrote:
> %python
>>>> import timeit
>>>> import numpy as np
> 
>>>> t=timeit.Timer('k = m - 0.5', setup='import numpy as np;m = 
>>>> np.ones([8092,8092],float); k = np.zeros(m.size, m.dtype)')
>>>> np.mean(t.repeat(repeat=10, number=1))
> 0.58557529449462886
> 
>>>> t=timeit.Timer('k = m - 0.5', setup='import numpy as np;m = 
>>>> np.ones([8092,8092],float)')
>>>> np.mean(t.repeat(repeat=10, number=1))
> 0.53153839111328127

I am surprised that there is any difference between these two approaches at 
all. I would have thought that in both cases a temporary array holding the 
result of m-0.5 is created, which is then assigned to the variable k. However, 
it seems that the second approach is about 10% faster (I see a similar 
difference on my machine). Why would that be?

On the other hand, if I actually use the preallocated space for k, and replace 
the assignment by

        k[…] = m - 0.5

then this takes about 40% more time, presumably because the content of the 
temporary array is copied into k (which must be initialized by m.shape instead 
of m.size in this case).

Thanks,

  Lutz

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to