# push! showing better performance than pre-allocation for Vector?

**URL:** <https://discourse.julialang.org/t/push-showing-better-performance-than-pre-allocation-for-vector/11136>\
**Category:** Performance\
**Created:** [May 24, 2018, 10:48pm UTC](https://discourse.julialang.org/t/push-showing-better-performance-than-pre-allocation-for-vector/11136 "2018-05-24T22:48:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shushman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shushman/32/4226_2.png) [@shushman](https://discourse.julialang.org/u/shushman)\
**Post date:** [May 24, 2018, 10:48pm UTC](https://discourse.julialang.org/t/push-showing-better-performance-than-pre-allocation-for-vector/11136/1 "2018-05-24T22:48:57Z")

</div>

I’ll just post the code snippets - they are quite self-explanatory. That said, I am still learning the nitty-gritties of Julia so please bear with me.

```julia
vect = Vector{Int}(1000000)
@time for i = 1:1000000
       vect[i] = i
       end
  0.021413 seconds (2.00 M allocations: 30.502 MiB, 4.70% gc time)

vect = Vector{Int}()
@time for i = 1:1000000
       push!(vect,i)
       end
  0.020107 seconds (999.51 k allocations: 24.252 MiB, 6.56% gc time)

```

push! is taking a little less time AND fewer allocations. What am I missing here? This was on a freshly started Julia REPL. Any help appreciated thanks!

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [May 24, 2018, 10:51pm UTC](https://discourse.julialang.org/t/push-showing-better-performance-than-pre-allocation-for-vector/11136/2 "2018-05-24T22:51:22Z")

</div>

These timings are not that representative due to [use of global variables](https://docs.julialang.org/en/stable/manual/performance-tips/#Avoid-global-variables-1). Try wrapping your code in a function.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 25, 2018, 6:13am UTC](https://discourse.julialang.org/t/push-showing-better-performance-than-pre-allocation-for-vector/11136/3 "2018-05-25T06:13:42Z")

</div>

```julia
julia> function foo()
           v = Vector{Float64}(1000000)
           for i = 1:1000000
               v[i] = rand()
           end
           v
       end
foo (generic function with 1 method)

julia> function bar()
           v = Vector{Float64}()
           for i = 1:1000000
               push!(v, rand())
           end
           v
       end
bar (generic function with 1 method)

julia> @btime foo();
  2.827 ms (2 allocations: 7.63 MiB)

julia> @btime bar();
  7.238 ms (20 allocations: 9.00 MiB)

```

---

<div class="post-metadata">

**Author:** ![shushman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shushman/32/4226_2.png) [@shushman](https://discourse.julialang.org/u/shushman)\
**Post date:** [May 25, 2018, 8:28am UTC](https://discourse.julialang.org/t/push-showing-better-performance-than-pre-allocation-for-vector/11136/4 "2018-05-25T08:28:27Z")

</div>

Thanks guys! I do avoid using global variables in general but did not realize that this also affects the timing of these two approaches.

---

<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:** [May 25, 2018, 10:35am UTC](https://discourse.julialang.org/t/push-showing-better-performance-than-pre-allocation-for-vector/11136/5 "2018-05-25T10:35:13Z")

</div>

Note also that the `rand()` call is not that cheap. If instead you assign the elements to `0.0`, so that you are just timing the difference in allocation, the difference is larger on my machine: a factor of 7.

Conversely, of course, in a real application where you are performing some expensive computation for each element, the difference in allocation cost will matter less.
