Hi everyone, I want to create a Dict() object where the keys are NTuple{6, Int} and the corresponding values are rank-6 arrays. I found the following function to create such Dict object with 64 items are much slower than I expected:
function test_time()
a = Dict()
for k in Iterators.product([0:1, 0:1, 0:1, 0:1, 0:1, 0:1]…)
a[k] = Array{Float64}(undef, 16, 16, 16, 16, 16, 16)
end
return a
end
julia> @btime test_time();
1.371 s (497 allocations: 8.00 GiB)
In the function above, I add 64 key-value pairs sequentially. But a single array initialization only cost little time :
Instead of using Dict{Any, Any} you can create the Dict with the proper types:
function test_time_new()
a = Dict{NTuple{6, Int64},Array{Float64}}()
for k in Iterators.product([0:1, 0:1, 0:1, 0:1, 0:1, 0:1]...)
a[k] = Array{Float64}(undef, 16, 16, 16, 16, 16, 16)
end
return a
end
function test_time_new2()
a = Dict{NTuple{6, Int64},Array{Float64, 6}}()
for k in Iterators.product([0:1, 0:1, 0:1, 0:1, 0:1, 0:1]...)
a[k] = Array{Float64}(undef, 16, 16, 16, 16, 16, 16)
end
return a
end
I observe similar timings on my laptop. Is there a chance that you don’t have enough available memory and your OS has to resort to the swap space? That might slow down the allocations.
I am not quite sure but it seems to be a version problem. I switch to Julia 1.7.2 on the same machine and the timing results looks good as @oheil. How can different versions result in such a huge difference if it is so