# Popfirst!() on an array

**URL:** <https://discourse.julialang.org/t/popfirst-on-an-array/29456>\
**Category:** New to Julia\
**Created:** [October 3, 2019, 4:32pm UTC](https://discourse.julialang.org/t/popfirst-on-an-array/29456 "2019-10-03T16:32:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)\
**Post date:** [October 3, 2019, 4:32pm UTC](https://discourse.julialang.org/t/popfirst-on-an-array/29456/1 "2019-10-03T16:32:12Z")

</div>

How efficient is `popfirst!() ` on an Array compared to `pop!()`? If I use `popfirst!()` do all of the elements in the array need to be shifted over?

If they do need shifting, it would need to read through the entire array. Am I correct in assuming that `pop!()` does not iterate through the entire array?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 3, 2019, 4:54pm UTC](https://discourse.julialang.org/t/popfirst-on-an-array/29456/2 "2019-10-03T16:54:45Z")

</div>

Here is a small benchmark:

```julia
julia> popper(N, f) = (b = rand(N); while !isempty(b) f(b) end)
popper (generic function with 2 methods)

julia> using BenchmarkTools

julia> @btime popper(10^6, popfirst!)
  4.328 ms (2 allocations: 7.63 MiB)

julia> @btime popper(10^6, pop!)
  2.943 ms (2 allocations: 7.63 MiB)

```

so it isn’t such a big difference.

---

<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:** [October 3, 2019, 5:10pm UTC](https://discourse.julialang.org/t/popfirst-on-an-array/29456/3 "2019-10-03T17:10:20Z")

</div>

In short, `popfirst!` and `pushfirst!` on `Array` will do the same sort of trickery that `pop!` and `push!` do — that is, they over allocate and simply change where the array “starts” to amortize the cost of the operations.

---

<div class="post-metadata">

**Author:** ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)\
**Post date:** [October 3, 2019, 5:17pm UTC](https://discourse.julialang.org/t/popfirst-on-an-array/29456/4 "2019-10-03T17:17:22Z")

</div>

If I keep doing `popfirst!` followed by `push!` enough times, so that I’m always pushing the element I just popped and not increasing the length of the array, would my `Array` eventually need to expand allocated space, or would it cycle through the space it already has reserved?

Edit: @mbauman because I think I did a general reply, rather than a reply to you

---

<div class="post-metadata">

**Author:** ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)\
**Post date:** [October 3, 2019, 5:19pm UTC](https://discourse.julialang.org/t/popfirst-on-an-array/29456/5 "2019-10-03T17:19:28Z")

</div>

Thank you for doing this! I will start using BenchmarkTools now, too. 😃
