# How to better initialize a dictionary containing multidimensional array?

**URL:** https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632
**Category:** Performance
**Tags:** performance, array, dictionaries
**Created:** [June 4, 2025, 7:59am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632 "2025-06-04T07:59:44Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Harrykjg-physics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harrykjg-physics/32/208708_2.png) [@Harrykjg-physics](https://discourse.julialang.org/u/Harrykjg-physics)
#### Post date: [June 4, 2025, 7:59am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632/1 "2025-06-04T07:59:45Z")

</div>

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 :

julia\> @btime Array{Float64}(undef, 16, 16, 16, 16, 16, 16);  
1.681 μs (2 allocations: 128.00 MiB)

And 1.371 s / 64 is much larger than 1.681 μs.

Can anybody help me with the problem, or provide better practices ?

Thanks in advance !

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [June 4, 2025, 8:13am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632/2 "2025-06-04T08:13:29Z")

</div>

Instead of using `Dict{Any, Any}` you can create the `Dict` with the proper types:

```julia
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

```

```julia
julia> @btime test_time();
  372.200 μs (564 allocations: 8.00 GiB)

julia> @btime test_time_new();
  256.400 μs (447 allocations: 8.00 GiB)

```

But I suspect that you may be able to overthink your data representation to gain much more performance.

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [June 4, 2025, 8:19am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632/3 "2025-06-04T08:19:32Z")

</div>

Further type restriction might help for down-stream tasks:

```julia
a = Dict{NTuple{6, Int64}, Array{Float64, 6}}()
                                       #^^^

```

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [June 4, 2025, 8:25am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632/4 "2025-06-04T08:25:20Z")

</div>

```julia
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

```

```julia
julia> @btime test_time();
  268.700 μs (564 allocations: 8.00 GiB)

julia> @btime test_time_new();
  259.400 μs (447 allocations: 8.00 GiB)

julia> @btime test_time_new2();
  257.900 μs (447 allocations: 8.00 GiB)

```

My first timings were wrong, the function are too volatile to window switches and mouse movings…

---

<div class="post-metadata">

### Author: ![Harrykjg-physics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harrykjg-physics/32/208708_2.png) [@Harrykjg-physics](https://discourse.julialang.org/u/Harrykjg-physics)
#### Post date: [June 4, 2025, 8:50am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632/5 "2025-06-04T08:50:47Z")

</div>

Thanks for your comments, your timing results are much more reasonable compared with mine, Is this a Julia version problem ? My version is 1.10.2

---

<div class="post-metadata">

### Author: ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)
#### Post date: [June 4, 2025, 9:10am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632/6 "2025-06-04T09:10:17Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Harrykjg-physics](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/harrykjg-physics/32/208708_2.png) [@Harrykjg-physics](https://discourse.julialang.org/u/Harrykjg-physics)
#### Post date: [June 4, 2025, 9:17am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632/7 "2025-06-04T09:17:35Z")

</div>

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 🙃

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [June 4, 2025, 9:48am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632/8 "2025-06-04T09:48:19Z")

</div>

> [@Harrykjg-physics](#):
>
> How can different versions result in such a huge difference

This is possible in general, but I lack the skill to connect your code to the former changes in versions.

My Julia version is 1.11.5

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [June 4, 2025, 9:49am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632/9 "2025-06-04T09:49:42Z")

</div>

> [@barucden](#):
>
> Is there a chance that you don’t have enough available memory and your OS

Looking at the timing differences at OPs and mine (plenty of RAM) this seems to be reasonable.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [June 4, 2025, 11:00am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632/10 "2025-06-04T11:00:14Z")

</div>

Can you explain what you want to do downstream with this dictionary?

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [June 4, 2025, 11:52am UTC](https://discourse.julialang.org/t/how-to-better-initialize-a-dictionary-containing-multidimensional-array/129632/11 "2025-06-04T11:52:38Z")

</div>

I see the same slow behavior on a machine with plenty of RAM (48 GB) and Julia 1.11.5.

```julia
julia> @b test_time()
315.842 ms (204 allocs: 8.000 GiB, 99.55% gc time, without a warmup)

julia> @b Array{Float64}(undef, 2^6, 16, 16, 16, 16, 16, 16) # allocate all in one step
168.777 μs (3 allocs: 8.000 GiB, 81.04% gc time)

julia> @b begin GC.enable(false); test_time(); GC.enable(true) end
232.363 μs (204 allocs: 8.000 GiB)

```

Most of the time is spent on garbage collection. I wonder why.
