I have written the following test to see the difference between going
copying element-by-element and using copy!(x,y):
function test1(x,y)
for i in eachindex(x)
@inbounds x[i] = y[i]
end
end
function test2(x,y)
copy!(x,y)
end
function test()
NumObs = 1000
x = rand(NumObs)
y = rand(NumObs)
test1(x,y)
test2(x,y)
println("Test 1")
@time(for z in 1:1e5 test1(x,y) end)
println("Test 2")
@time(for z in 1:1e5 test2(x,y) end)
end
test()
I get the following timings:
Test 1
0.031750 seconds
Test 2
0.009360 seconds
So it seems copy!() is quite a bit faster ...
I ran this test because I would like to copy an element of a vector y into
an element of vector x, but x and y are not the same everywhere - so i
can’t do copy!(x,y). Moreover, since copy!() only works on arrays, I can’t
do copy!(x[i],y[i]).
So my question is whether there is a way to get the speed of copy!() , but
for copying a Float to a Float? Seems like i am probably missing something
fairly simple...
Thanks
Alan