What is the fastest way to perform unary operation inplace ?
I have tried the followings
b = rand(1000000)
@time b = -b
# 0.011017 seconds (2 allocations: 7.629 MiB)
@time b = (-).(b)
# 0.012952 seconds (2 allocations: 7.629 MiB)
@time b .-= 2 .* b
# 0.002173 seconds
@time begin
for i in 1:length(b)
@inbounds b[i] = -b[i]
end
end
# 0.001698 seconds
the first and the second allocate a new array, the thrid use a trick that cannot be extended to all unary operators and the fourth is not as concise as one would like it to be.