I have a vector with a million floats, and some missing values. I want to say that it’s a vector of meters. On Julia 1.0.3:
julia> using BenchmarkTools, Unitful
julia> v = vcat(fill(1.0, 500000), missing, fill(1.0, 500000));
julia> import Base: *
julia> *(::Missing, ::Unitful.Units) = missing
* (generic function with 400 methods)
julia> @btime v .* u"m";
60.390 ms (999502 allocations: 31.46 MiB)
julia> @btime copy(v); # for comparison
1.892 ms (2 allocations: 8.58 MiB)
On Julia 1.1 it should be faster, thanks to efforts by @nalimilan. However, what I would really like is to avoid the copy altogether. This should be a 5 ns operation. I thought of reinterpret
, but unfortunately:
julia> reinterpret(Union{typeof(1.0u"m"), Missing}, v)
ERROR: ArgumentError: cannot reinterpret `Union{Missing, Float64}` `Union{Missing, Quantity{Float64,𝐋,FreeUnits{(m,),𝐋,nothing}}}`, type `Union{Missing, Quantity{Float64,𝐋,FreeUnits{(m,),𝐋,nothing}}}` is not a bits type
Stacktrace:
[1] (::getfield(Base, Symbol("#throwbits#184")))(::Type{Union{Missing, Float64}}, ::Type{Union{Missing, Quantity{Float64,𝐋,FreeUnits{(m,),𝐋,nothing}}}}, ::Type{Union{Missing, Quantity{Float64,𝐋,FreeUnits{(m,),𝐋,nothing}}}}) at ./reinterpretarray.jl:16
[2] reinterpret(::Type{Union{Missing, Quantity{Float64,𝐋,FreeUnits{(m,),𝐋,nothing}}}}, ::Array{Union{Missing, Float64},1}) at ./reinterpretarray.jl:33
[3] top-level scope at none:0
Did I miss anything?