Consider the following variations of in-place copying:
lower = 1.:3
upper = 12.:16
function a()
points = zeros(100)
allocs = @allocated copy!(points, [lower[1:end-1]; upper[1:end-1]])
println("case a: $allocs bytes")
resize!(points, length(lower)+length(upper)-2)
end
function b()
points = zeros(100)
allocs = @allocated begin
copy!(points, lower[1:end-1])
copy!(points, length(lower), upper[1:end-1])
end
println("case b: $allocs bytes")
resize!(points, length(lower)+length(upper)-2)
end
function c()
points = zeros(100)
allocs = @allocated begin
copy!(points, @view(lower[1:end-1]))
copy!(points, length(lower), @view(upper[1:end-1]))
end
println("case c: $allocs bytes")
resize!(points, length(lower)+length(upper)-2)
end
The result that i get (in the second run), is:
julia> a(); b(); c();
case a: 320 bytes
case b: 192 bytes
case c: 1728 bytes
- In case a, does
[lower[1:end-1]; upper[1:end-1]]
as a second argument tocopy!
create a temporary vector, while in case b it doesn’t, explaining that they differ roughly in twice as many allocations? - For case c, why does it allocate that much, shouldn’t it be the same as case b?