# empty matrix creation

**URL:** <https://discourse.julialang.org/t/empty-matrix-creation/18910>\
**Category:** Performance\
**Created:** [December 21, 2018, 7:18pm UTC](https://discourse.julialang.org/t/empty-matrix-creation/18910 "2018-12-21T19:18:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kehlert](https://avatars.discourse-cdn.com/v4/letter/k/6bbea6/32.png) [@kehlert](https://discourse.julialang.org/u/kehlert)\
**Post date:** [December 21, 2018, 7:18pm UTC](https://discourse.julialang.org/t/empty-matrix-creation/18910/1 "2018-12-21T19:18:07Z")

</div>

I am trying to fill a matrix, and currently it takes longer to create it than to fill it. Here is the code for creating it:

```julia
Array{Int64,2}(undef, 3, 10^3)

```

I profiled it, and it seems to spend all of its time in the Type() function in boot.jl. Am I doing something wrong here?

---

<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:** [December 21, 2018, 7:21pm UTC](https://discourse.julialang.org/t/empty-matrix-creation/18910/2 "2018-12-21T19:21:29Z")

</div>

Can you show your benchmarking code and the results?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 21, 2018, 7:26pm UTC](https://discourse.julialang.org/t/empty-matrix-creation/18910/3 "2018-12-21T19:26:00Z")

</div>

> [@kehlert](#):
>
> I am trying to fill a matrix, and currently it takes longer to create it than to fill it.

Yes, on my machine memory allocation is slower than initializing. For example,

```jl
@btime fill(0, (3,1000));
A = Array{Int,2}(undef, 3, 1000)
@btime fill!($A, 0);

```

gives

```julia
1.383 μs (2 allocations: 23.52 KiB)
446.586 ns (0 allocations: 0 bytes)

```

Maybe this isn’t an entirely fair comparison since in benchmarking the `fill!` call, the array `A` is in-cache (because `@btime` calls `fill!` over and over on the same array and reports the fastest time). On the other hand, improving cache locality is a real benefit of pre-allocating arrays.

(Obviously, if your initialization is significantly more complicated than simply setting the entries to zero, then the initialization time could become more important.)
