# Different ways to initialize an array of specified type

**URL:** https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818
**Category:** General Usage
**Created:** [July 28, 2020, 3:23pm UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818 "2020-07-28T15:23:10Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)
#### Post date: [July 28, 2020, 3:23pm UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818/1 "2020-07-28T15:23:11Z")

</div>

Let’s benchmark the following three:

```julia
julia> function a()
           x = Vector{Int}()
           x
       end
a (generic function with 1 method)

julia> function b()
           x::Vector{Int} = []
           x
       end
b (generic function with 1 method)

julia> function c()
           x = Int[]
           x
       end
c (generic function with 1 method)

```

```julia
julia> @benchmark a()
BenchmarkTools.Trial:
  memory estimate: 80 bytes
  allocs estimate: 1
  --------------
  minimum time: 16.800 ns (0.00% GC)
  median time: 18.700 ns (0.00% GC)
  mean time: 21.590 ns (7.51% GC)
  maximum time: 1.045 μs (97.54% GC)
  --------------
  samples: 10000
  evals/sample: 1000

julia> @benchmark b()
BenchmarkTools.Trial:
  memory estimate: 160 bytes
  allocs estimate: 2
  --------------
  minimum time: 44.209 ns (0.00% GC)
  median time: 46.727 ns (0.00% GC)
  mean time: 52.021 ns (7.29% GC)
  maximum time: 1.514 μs (94.97% GC)
  --------------
  samples: 10000
  evals/sample: 993

julia> @benchmark c()
BenchmarkTools.Trial:
  memory estimate: 80 bytes
  allocs estimate: 1
  --------------
  minimum time: 17.116 ns (0.00% GC)
  median time: 19.019 ns (0.00% GC)
  mean time: 22.369 ns (8.91% GC)
  maximum time: 1.356 μs (97.95% GC)
  --------------
  samples: 10000
  evals/sample: 999

```

What makes `b()` so much slower than `a()`? Are `a()` and `c()` equivalent, or the difference of 2-5% really exists?

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [July 28, 2020, 3:34pm UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818/2 "2020-07-28T15:34:32Z")

</div>

> [@anon37204545](#):
>
> `Vector{Int}[]`

This is an empty vector of vectors, while `Int[]` is a vector of integers.

And `[]` is an empty vector of Any, which you then convert to `Int[]` in (b), which is why it allocates twice.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 28, 2020, 3:35pm UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818/3 "2020-07-28T15:35:13Z")

</div>

In `b()` you are initializing an array of with eltype `Any` and then converting it to an array with eltype `Int`. My guess is that it has to re-allocate memory and make a fresh array. Thus the time is around double the normal times.

---

<div class="post-metadata">

### Author: ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)
#### Post date: [July 28, 2020, 3:46pm UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818/4 "2020-07-28T15:46:44Z")

</div>

Looking at `@code_native a()` etc. I think `a` and `c` are equivalent, whereas `b` needs to work much more.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 28, 2020, 3:51pm UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818/5 "2020-07-28T15:51:39Z")

</div>

Actually, `a` and `c` do different things (as @mcabbott pointed out): `a()` creates a vector-of-vectors while `c()` just creates a vector.

@anon37204545 I suspect you meant to do `Vector{Int}()` in `a()` instead of `Vector{Int}[]`.

---

<div class="post-metadata">

### Author: ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)
#### Post date: [July 28, 2020, 3:55pm UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818/6 "2020-07-28T15:55:23Z")

</div>

> [@rdeits](#):
>
> `a()` creates a vector-of-vectors while `c()` just creates a vector.

You’re right, thanks, I overlooked that.

---

<div class="post-metadata">

### Author: ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)
#### Post date: [July 28, 2020, 4:21pm UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818/7 "2020-07-28T16:21:36Z")

</div>

> [@mcabbott](#):
>
> This is an empty vector of vectors, while `Int[]` is a vector of integers.

Thanks, fixed. (This doesn’t change the performance.)

> [@mcabbott](#):
>
> And `[]` is an empty vector of Any, which you then convert to `Int[]` in (b), which is why it allocates twice.

So in general, initializing an empty array should be done in the manner of `a()` or `c()`?

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [July 28, 2020, 4:35pm UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818/8 "2020-07-28T16:35:12Z")

</div>

Yes. But if you are counting the nanoseconds, then you probably don’t want to be creating empty arrays at all. `push!` is pretty clever but making the array the right size the first time is better. And re-using an array made outside the bit where nanoseconds count is even better.

---

<div class="post-metadata">

### Author: ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)
#### Post date: [July 28, 2020, 4:39pm UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818/9 "2020-07-28T16:39:18Z")

</div>

I know setting the right size is also more performant. But does it matter if a vector can have between 3 and 8 elements at the end? In that case, I can’t pre-allocate 8 undef elements to the array.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [July 28, 2020, 4:48pm UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818/10 "2020-07-28T16:48:32Z")

</div>

It could be faster to give it length 8 up front, fill what you fill, and `resize!` afterwards. Worth a shot anyway.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 29, 2020, 8:09am UTC](https://discourse.julialang.org/t/different-ways-to-initialize-an-array-of-specified-type/43818/11 "2020-07-29T08:09:05Z")

</div>

Or possibly use `sizehint!` up front, and then just `push!`.
