El martes, 21 de octubre de 2014 14:25:33 UTC-5, Roy Wang escribió:
>
> This is great, thanks!
>
> I verified pointer(X) is the same as what pointer(x_next) used to be 
> before running your line of code, and pointer(x_next) after running your 
> line of code is the same as pointer(x).
>
> I did not know of this about Julia!
>

I'm glad this was useful!

This is a common idiom (programming style / trick) in any language. 
Unfortunately, often people don't know 
that this is possible or practical, and they waste a lot of time copying 
arrays unnecessarily when
they could just be swapping the pointers, as you put it.

In most languages you would need to call an explicit function, like `swap` 
in C++, in order to do this.
This idiom,   (x, x_next = x_next, x)   works in both Python and Julia. 
Yes, it is quite neat ;)

[The capital X that was in my original post was due to an auto-correct when 
typing from my phone.
It should be small x to work with the code in your loop.]

David.

 

>
> On Saturday, 4 October 2014 11:05:33 UTC-4, David P. Sanders wrote:
>>
>> Hi, 
>>
>> Are you sure that you need a copy operation? If I understand correctly 
>> what you are doing, you just need access to the x from the previous 
>> iteration. 
>>
>> Could you not just swap x and x_next and avoid the copy :
>>
>> X, x_next = x_next, x
>>
>> (so you create them once only). 
>>
>>

Reply via email to