What's the best way to build a Julia array element by element when I am not sure of the final array size? In my problem, I am building the array in a single for loop.
So far, I have figured out I can either push into the array or index into a pre-allocated array:
using BenchmarkTools
function push_to_array!(array, N)
for i in 1:N
# some computation
push!(array, rand(Int64))
end
end
function fill_array!(array, N)
for i in 1:N
# some computation
array[i]=rand(Int64)
end
end
N = 100_000_000 # unknown in the real problem
empty = Vector{Int64}()
preallocated = Vector{Int64}(undef, 2*N) # 2*N represents some upper bound on N
@btime push_to_array!(empty, N)
# 28.272 s (6 allocations: 0 bytes)
@btime fill_array!(preallocated, N)
# 2.449 s (0 allocations: 0 bytes)
So filling a pre-allocated array is a lot faster than pushing, however, it's a bit cumbersome as I need to trim the output with correct_size = preallocated[1:N].
Is there a faster/better way of doing this?
sizehint!on the output array before you start pushing.