Difference between copy() and .= for two vectors

I need to copy the content of one vector to another that already exists, in this case is better to use .= instead of the copy() function?

if you use copy(), you’re effectively moving data twice:

a .= copy(b)

is the same as:

c = copy(b)
a .= c

so you should be better of just doing:

a .= b

of course, this is assuming you don’e need deepcopy()

Sorry, I think a didn’t express myself correctly, my intent was comparing
a = copy(b)
And
a .= b

Perhaps you mean copyto!?

2 Likes

There shouldn’t be any difference as such, as both the broadcast assignment and copy internally use copyto!

1 Like

Are you sure this is what you really meant? a .= b writes data into a pre-existing array, while copy must allocate a new one.

The more meaningful comparison would be a .= b and copyto!(a, b)

3 Likes

Would be better just use copyto!(a,b) in this case then?

1 Like