# Preallocating Vector of Vectors

**URL:** <https://discourse.julialang.org/t/preallocating-vector-of-vectors/69304>\
**Category:** Performance\
**Tags:** memory-allocation, arrays\
**Created:** [October 6, 2021, 1:22pm UTC](https://discourse.julialang.org/t/preallocating-vector-of-vectors/69304 "2021-10-06T13:22:30Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 6, 2021, 7:06pm UTC](https://discourse.julialang.org/t/preallocating-vector-of-vectors/69304/21 "2021-10-06T19:06:34Z")

</div>

> [@Jeff\_Emanuel](#):
>
> There are two different `makearr2` 's in this thread.

Using the original OP’s:

```julia
function makearr2(T, Nrow, Ncol)
    return [Vector{T}(undef, Nrow) for _ in 1:Ncol]
end

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 6, 2021, 7:23pm UTC](https://discourse.julialang.org/t/preallocating-vector-of-vectors/69304/22 "2021-10-06T19:23:15Z")

</div>

> [@lmiq](#):
>
> `function makearr4(::Val{T},`

I think the most idiomatic would be `::Type{T}`, though it probably doesn’t make any performance difference.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [October 6, 2021, 8:00pm UTC](https://discourse.julialang.org/t/preallocating-vector-of-vectors/69304/23 "2021-10-06T20:00:39Z")

</div>

I have the suspicion this is variation of our recent discussion [How to do custom allocators](https://discourse.julialang.org/t/how-to-do-custom-allocators/69112). ArrayOfArrays.jl probably uses some custom allocators, too.

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [October 7, 2021, 6:58am UTC](https://discourse.julialang.org/t/preallocating-vector-of-vectors/69304/24 "2021-10-07T06:58:58Z")

</div>

If your vectors all have the same size, you could try zeros(m,n) and then go with  
[Zero-Cost Abstractions in Julia: Indexing Vectors by Name with LabelledArrays - Stochastic Lifestyle](https://www.stochasticlifestyle.com/zero-cost-abstractions-in-julia-indexing-vectors-by-name-with-labelledarrays/)

---

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [October 7, 2021, 12:34pm UTC](https://discourse.julialang.org/t/preallocating-vector-of-vectors/69304/25 "2021-10-07T12:34:31Z")

</div>

> [@lmiq](#):
>
> ```julia
> function makearr4(::Val{T}, Nrow, Ncol) where T
> [Vector{T}(undef, Ncol) for _ in 1:Nrow]
> end
> 
> ```

This is a really good tip with `push!`, and I would not have expected that if I hadnt seen that. Thank you!

---

<div class="post-metadata">

**Author:** ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)\
**Post date:** [October 7, 2021, 12:38pm UTC](https://discourse.julialang.org/t/preallocating-vector-of-vectors/69304/26 "2021-10-07T12:38:14Z")

</div>

> [@lmiq](#):
>
> `Val(Float64)`

I ended up with

```julia
function VectorOfVector(::Type{T}, Nrow::Integer, Ncol::Integer) where {T}
    return [Vector{T}(undef, Nrow) for _ in Base.OneTo(Ncol)]
end

```

which is basically the same as your version. Thank you!

[Previous page](https://discourse.julialang.org/t/preallocating-vector-of-vectors/69304.md?page=1)
