# How fast is \`unshift!\` for a \`Vector\`?

**URL:** https://discourse.julialang.org/t/how-fast-is-unshift-for-a-vector/4542
**Category:** General Usage
**Created:** [June 29, 2017, 2:11pm UTC](https://discourse.julialang.org/t/how-fast-is-unshift-for-a-vector/4542 "2017-06-29T14:11:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [June 29, 2017, 2:11pm UTC](https://discourse.julialang.org/t/how-fast-is-unshift-for-a-vector/4542/1 "2017-06-29T14:11:47Z")

</div>

`unshift!` adds an element at the front of a `Vector`, which requires shifting the whole contents of the `Vector` to the right. This feels like is should be slow (or, rather, slooow).

However, some naive benchmarking indicates otherwise:

```julia
using BenchmarkTools

julia 0.6> @btime push!(v, 1) samples=1000 evals=1000;
  25.801 ns (0 allocations: 0 bytes)

julia 0.6> @btime unshift!(v, 1) samples=1000 evals=1000;
  26.104 ns (0 allocations: 0 bytes)

```

Is `unshift!` actually as fast as this would indicate?

---

<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: [June 29, 2017, 2:27pm UTC](https://discourse.julialang.org/t/how-fast-is-unshift-for-a-vector/4542/2 "2017-06-29T14:27:43Z")

</div>

> [@dpsanders](#):
>
> unshift! adds an element at the front of a Vector, which requires shifting the whole contents of the Vector to the right. This feels like is should be slow (or, rather, slooow).

No, the `Array` implementation in Base is designed to efficiently insert/delete items at both ends of the array. See the [code to grow the beginning of the array](https://github.com/JuliaLang/julia/blob/82df8e794b828b76ab74b73e0fe4b649c2d359fb/src/array.c#L672-L739), for example. When items are inserted, it doubles the allocated length of the array as needed and leaves some unused space at the beginning for inserting subsequent items. This means that repeatedly inserting items at either end has O(1) amortized cost.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [June 29, 2017, 2:30pm UTC](https://discourse.julialang.org/t/how-fast-is-unshift-for-a-vector/4542/3 "2017-06-29T14:30:23Z")

</div>

Though a demonstration of @stevengj’s answer is redundant:

```julia
julia> pointer(v)
Ptr{Float64} @0x00007f24867610b0

julia> unshift!(v,1);

julia> pointer(v)
Ptr{Float64} @0x00007f24867610a8

```

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [June 29, 2017, 2:32pm UTC](https://discourse.julialang.org/t/how-fast-is-unshift-for-a-vector/4542/4 "2017-06-29T14:32:51Z")

</div>

Great, thanks!
