# Vcat vs push!, which one is more efficient

**URL:** <https://discourse.julialang.org/t/vcat-vs-push-which-one-is-more-efficient/48353>\
**Category:** New to Julia\
**Created:** [October 14, 2020, 9:49am UTC](https://discourse.julialang.org/t/vcat-vs-push-which-one-is-more-efficient/48353 "2020-10-14T09:49:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fdamore](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@fdamore](https://discourse.julialang.org/u/fdamore)\
**Post date:** [October 14, 2020, 9:49am UTC](https://discourse.julialang.org/t/vcat-vs-push-which-one-is-more-efficient/48353/1 "2020-10-14T09:49:58Z")

</div>

Hi everybody,  
I’m still a Julia newbie, so don’t be angry at me for silly questions.

I’m wondering whenever I should use vcat or push! to append elements to arrays. For example, let’s say I have one array  
a = zeros(100)  
and I want to append an element equal to 1, should I use a = vcat(a,[1]) or push!(a,1)?

At a first glance, it seems the same, but if I try to iterate this process, i.e. I want to append 100 times different elements, which are not given _a priori_ (for simplicity, assume we always append 1), push! seems to be faster than vcat.

If, instead, the elements are given _a priori_, then concatenating the two arrays using once vcat seems to be faster than iteratively using push! to append elements of the second array to the first.

What it the reason behind this behavior?

Thanks!

---

<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:** [October 14, 2020, 9:51am UTC](https://discourse.julialang.org/t/vcat-vs-push-which-one-is-more-efficient/48353/2 "2020-10-14T09:51:14Z")

</div>

They do different things; `vcat` creates a new array, `push!` modifies the array. It sounds like you want `push!` (or `append!`).

---

<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:** [October 14, 2020, 10:19am UTC](https://discourse.julialang.org/t/vcat-vs-push-which-one-is-more-efficient/48353/3 "2020-10-14T10:19:56Z")

</div>

In the second case, use `append!(a, b)` to modify `a` by concatenating `b` to it. That should be as fast as or faster than `vcat`.

`push!` and `append!` are designed to be “cheap” in the amortized sense: arrays typically have some extra capacity beyond their current length, which can be used to add more elements without extra memory allocation. If that extra capacity is not enough, Julia may try to see if there is unused memory right after the array, in which case it can extend the array without copying the existing data into other place, which, again, is cheaper than moving all data on each operation. That makes `push!` and `append!` complexity “amortized linear” in number of added elements.

`vcat`, on the other hand, is “pure” function, i.e. it does not modify the object passed to it but allocates a new array each time and copies the data. Hence, if you add elements by one via `vcat`, then the complexity is quadratic in number of added elements.

---

<div class="post-metadata">

**Author:** ![fdamore](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@fdamore](https://discourse.julialang.org/u/fdamore)\
**Post date:** [October 14, 2020, 10:38am UTC](https://discourse.julialang.org/t/vcat-vs-push-which-one-is-more-efficient/48353/5 "2020-10-14T10:38:25Z")

</div>

Thank you very much, this explanation made things more clear. Then, in my case, I should use push! (since I do not have `a priori` knowledge of the second array).
