# Creating an array

**URL:** <https://discourse.julialang.org/t/creating-an-array/128113>\
**Category:** New to Julia\
**Created:** [April 16, 2025, 6:30am UTC](https://discourse.julialang.org/t/creating-an-array/128113 "2025-04-16T06:30:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 16, 2025, 6:30am UTC](https://discourse.julialang.org/t/creating-an-array/128113/1 "2025-04-16T06:30:08Z")

</div>

Hello,  
Which of the following methods is best for creating an array?

```julia
Array1 = fill(0,100)
Array2 = Array{Int64}(undef,100)

```

Thank you.

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [April 16, 2025, 6:36am UTC](https://discourse.julialang.org/t/creating-an-array/128113/2 "2025-04-16T06:36:39Z")

</div>

The top one is slower because it fills with zeros, the bottom one is faster because it only allocates memory.

```julia
julia> @btime Array1 = fill(0,100)
  30.242 ns (1 allocation: 896 bytes)
julia> @btime Array2 = Array{Int64}(undef,100)
  13.413 ns (1 allocation: 896 bytes)

```

If you don’t need the 0s, consider the latter.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [April 16, 2025, 6:49am UTC](https://discourse.julialang.org/t/creating-an-array/128113/3 "2025-04-16T06:49:06Z")

</div>

Hi,  
Thank you so much.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [April 16, 2025, 7:01am UTC](https://discourse.julialang.org/t/creating-an-array/128113/4 "2025-04-16T07:01:12Z")

</div>

“Best” depends on your needs.

`zeros` creates an array with predictable contents.

`Array{T}(undef, n)` creates an uninitialized array, so that you have to make sure to initialize the elements before using them.

I’d recommend to at least start with the former in most cases as using uninitialized values is a way to heisenbugs. Zeros will at least give predictably wrong answers.

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [April 16, 2025, 11:30am UTC](https://discourse.julialang.org/t/creating-an-array/128113/5 "2025-04-16T11:30:58Z")

</div>

> [@karei](#):
>
> If you don’t need the 0s, consider the latter.

I would very much turn that around and say “If you don’t need the performance, consider the former” (or, consider [`zeros`](https://docs.julialang.org/en/v1/base/arrays/#Base.zeros) for a cleaner syntax).

Uninitialized memory is often a nasty source of bugs and you should be very careful when using it.
