[SOLVED] Why is allocating an array of Union{T, Missing} an order of magnitude slower than an array of T?

Issue

Allocating an array of Union{T, Missing} is very expensive in Julia. Is there any workaround it?

julia> @time Vector{Union{Missing, Int}}(undef, 10^7);
  0.031052 seconds (2 allocations: 85.831 MiB)

julia> @time Vector{Union{Int}}(undef, 10^7);
  0.000027 seconds (3 allocations: 76.294 MiB)

Solution

We had the same problem and as a workaround we used

x = Vector{Union{T,Missing}}(undef,1)
resize!(x, newlen)

Answered By – sl-solution

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *