# Performance of zeros() vs. Array{T}()?

**URL:** https://discourse.julialang.org/t/performance-of-zeros-vs-array-t/14596
**Category:** Performance
**Created:** [September 5, 2018, 7:17pm UTC](https://discourse.julialang.org/t/performance-of-zeros-vs-array-t/14596 "2018-09-05T19:17:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [September 5, 2018, 7:17pm UTC](https://discourse.julialang.org/t/performance-of-zeros-vs-array-t/14596/1 "2018-09-05T19:17:53Z")

</div>

In the following function, I see 1.56X speedup when using `zeros()` function for one input instead of using `Array{T}()`, which seems odd to me. When I use `@btime` though, I get the same timings.

```julia
julia> function vander(v, x, N::Int)
         M = length(x)
         if N > 0
           v[:,1] .= 1
         end
         if N > 1
           for i = 2:N
             v[:,i] = x
           end
           accumulate(v, v)
         end
         return v
       end
vander (generic function with 1 method)

julia> function accumulate(input, output)
         M, N = size(input)
         for i = 2:N
           for j = 1:M
             output[j,i] *= input[j,i-1]
           end
         end
       end
accumulate (generic function with 1 method)

```

Now compare the timings of `f()` and `g()`:

```julia
julia> function f()
         M, N = 10^8, 4
         x = rand(M)
         v = Array{Float64}(undef,M,N) # <-----
         t = @elapsed vander(v, x, N)
       end
f (generic function with 1 method)

julia> f()
1.2380960570000001

julia> function g()
         M, N = 10^8, 4
         x = rand(M)
         v = zeros(M,N) # <-----
         t = @elapsed vander(v, x, N)
       end
g (generic function with 1 method)

julia> g()
0.77949281

```

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [September 5, 2018, 7:52pm UTC](https://discourse.julialang.org/t/performance-of-zeros-vs-array-t/14596/2 "2018-09-05T19:52:13Z")

</div>

The overall function timing is telling you the whole story here —

For large uninitialized arrays, the operating system will sometimes lie to you and give you back a pointers to some space but it won’t have actually done any of the dirty work of allocating it for you. Zeros pays that cost for you upon writing zero to every element. The uninitialized `Array` constructor can sometimes defer that cost to the first time you write to it (or even to each page). See, e.g.:

> <https://stackoverflow.com/questions/911860/does-malloc-lazily-create-the-backing-pages-for-an-allocation-on-linux-and-othe>

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [September 5, 2018, 8:01pm UTC](https://discourse.julialang.org/t/performance-of-zeros-vs-array-t/14596/3 "2018-09-05T20:01:03Z")

</div>

Many thanks, I was thinking about something like this.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [September 6, 2018, 2:06am UTC](https://discourse.julialang.org/t/performance-of-zeros-vs-array-t/14596/4 "2018-09-06T02:06:53Z")

</div>

Am I reading this wrong? It seems to me that the timing indicated that the code was faster when  
the array `v` was created with `zeros`. Only the function `vander` was timed. So why was it faster?  
`vander` initializes `v`. Why would it matter how `v` was initialized (or not) before it was passed to `vander` ?

EDIT: by the way I am getting a speed up of 2.5 for using an array initialized to 0.0 instead of not initialized.

```julia
Julia Version 0.7.0
Commit a4cb80f3ed (2018-08-08 06:46 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-6650U CPU @ 2.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, skylake)

```

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [September 6, 2018, 2:26am UTC](https://discourse.julialang.org/t/performance-of-zeros-vs-array-t/14596/5 "2018-09-06T02:26:32Z")

</div>

Sorry, I just got it. @mbauman is saying that the time is spent either up-front (initialized array), or later (uninitialized array). My timings of the entire f() or g() confirm that. Interesting…
