For this type you should be memory bound so they should perform roughly equal. I increased the length of the arrays a bit to reduce noise and got:
Test 1 0.307454 seconds Test 2 0.293684 seconds On Wednesday, October 14, 2015 at 5:53:58 PM UTC+2, Stefan Karpinski wrote: > > Copy uses memmove which uses hand-coded assembly for optimal data copy > performance. > > On Wed, Oct 14, 2015 at 9:08 PM, Alan Crawford <[email protected] > <javascript:>> wrote: > >> 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 >> >> >
