Hi there,
suppose that I have a Float64
vector x
. I wish to identify non-zero elements of this vector. Below are my proposal for that.
f1!(y,x) = y .= (x .!=0 )[:];
function f2!(y,x)
@inbounds @simd for i ∈ eachindex(x)
if x[i]!=0
y[i] = true
end
end
end
q = 43416;
x = Array(sprand(Float64, 1, q, 0.75));
y = falses(q);
@btime f1!($y,$x);
126.599 μs (5 allocations: 15.14 KiB)
@btime f2!($y,$x);
133.500 μs (0 allocations: 0 bytes)
As you can see, f1!
is a bit faster than f2!
. However, it requires a few allocations. So, my question here is whether it is possible to write a function that is as fast as f1 and does not require allocations? Thank you very much in advance!