[Numpy-discussion] memory allocation at assignment

astronomer shailendra.vikas@gmail....
Wed Jun 27 22:46:07 CDT 2012


Hi Nathaniel,
Thanks for the clearing my understand. This is exactly what i needed.

Thanks,


Nathaniel Smith wrote:
> 
> On Thu, Jun 28, 2012 at 12:38 AM, astronomer <shailendra.vikas@gmail.com>
> wrote:
>>
>> Hi All,
>> I am wondering if there any difference in memory overhead between the
>> following code.
>> a=numpy.arange(10)
>> b=numpy.arange(10)
>> c=a+b
>>
>> and
>> a=numpy.arange(10)
>> b=numpy.arange(10)
>> c=numpy.empty_likes(a)
>> c[:]=a+b
>>
>> Does the later code make a temproray array for the result of (a+b) and
>> then
>> copy it to c. I beleive it does that, but i wanted to make sure.
> 
> Yes it does. If you want to avoid this extra copy, and have a
> pre-existing output array, you can do:
> 
> np.add(a, b, out=c)
> 
> ('+' on numpy array's is just a synonym for np.add; np.add is a ufunc,
> and all ufunc's accept this syntax:
>   http://docs.scipy.org/doc/numpy/reference/ufuncs.html
> )
> 
> -n
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 
> 

-- 
View this message in context: http://old.nabble.com/memory-allocation-at-assignment-tp34083731p34084248.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.



More information about the NumPy-Discussion mailing list