# How slow push! is?

**URL:** <https://discourse.julialang.org/t/how-slow-push-is/424>\
**Category:** General Usage\
**Tags:** question, faq\
**Created:** [November 18, 2016, 11:08pm UTC](https://discourse.julialang.org/t/how-slow-push-is/424 "2016-11-18T23:08:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)\
**Post date:** [November 18, 2016, 11:08pm UTC](https://discourse.julialang.org/t/how-slow-push-is/424/1 "2016-11-18T23:08:03Z")

</div>

suppose you need to build an array of unknown size. elements come in one by one, then an eof comes. the question is, how bad push!() is from performance viewpoint? the performant way to do this is to preallocate the array to a reasonable size, and grow it by chunks as needed, all while keeping track of the actual item count. can i expect Julia to kinda do this for me in the background with push!() or the array will be physically reallocated every time?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 18, 2016, 11:13pm UTC](https://discourse.julialang.org/t/how-slow-push-is/424/2 "2016-11-18T23:13:20Z")

</div>

The vector is grown by a constant factor every time.

---

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 18, 2016, 11:33pm UTC](https://discourse.julialang.org/t/how-slow-push-is/424/3 "2016-11-18T23:33:07Z")

</div>

Given that the question doesn’t specify the **scale** of speed, it’s worthy to point out that `push!`, although being much slower than preallocation, is still considerably faster than any I/O read. If the eof is expected e.g. from a file in the hard disk, I wouldn’t bother optimizing the RAM side of the array building process.

---

<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:** [November 18, 2016, 11:36pm UTC](https://discourse.julialang.org/t/how-slow-push-is/424/4 "2016-11-18T23:36:28Z")

</div>

`push!` is reasonably fast. It grows the array allocation geometrically as needed (currently by a factor of 2). As a result, pushing `n` elements one at a time to an empty array has `O(n)` cost and allocates `O(n)` storage total — only `O(log n)` physical re-allocations are performed.

I feel like this should be documented in the help for `push!`, since it seems to be a FAQ.
