# Is \`empty!\` O(1)?

**URL:** https://discourse.julialang.org/t/is-empty-o-1/122367
**Category:** Performance
**Created:** [November 7, 2024, 5:40am UTC](https://discourse.julialang.org/t/is-empty-o-1/122367 "2024-11-07T05:40:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![guoyongzhi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guoyongzhi/32/28306_2.png) [@guoyongzhi](https://discourse.julialang.org/u/guoyongzhi)
#### Post date: [November 7, 2024, 5:40am UTC](https://discourse.julialang.org/t/is-empty-o-1/122367/1 "2024-11-07T05:40:29Z")

</div>

[The doc page](https://docs.julialang.org/en/v1/base/collections/#Base.sizehint!) says that

> `empty!` is nearly costless (and O(1)) for types that support this kind of preallocation.

But the code of [`empty!`](https://github.com/JuliaLang/julia/blob/8f5b7ca12ad48c6d740e058312fc8cf2bbe67848/base/array.jl#L2016) and [`_deleteend!`](https://github.com/JuliaLang/julia/blob/8f5b7ca12ad48c6d740e058312fc8cf2bbe67848/base/array.jl#L1195) show that it’s in O(n) complexity. The [`_unsetindex!`](https://github.com/JuliaLang/julia/blob/671cd5e1db70322d043680336d96259553e7f023/base/genericmemory.jl#L78) in iteration body seems to do some gc reference counting job. However, is this necessary for a primitive type like `Float64`?

I also did some simple benchmark:

```julia
julia> a = ones(100000); @time empty!(a);

0.000010 seconds

julia> a = ones(1000000); @time empty!(a);

0.000042 seconds

julia> a = ones(10000000); @time empty!(a);

0.000516 seconds

julia> a = ones(100000000); @time empty!(a);

0.003783 seconds

```

It does look like in O(n) (linear time complexity).

I consider that the only thing `empty!(::Vector{Float64})` should do is to reset the “end pointer” to the first element and the length to 0, which is O(1).

Did I miss something? How to reuse a `Vector` efficiently?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [November 7, 2024, 5:45am UTC](https://discourse.julialang.org/t/is-empty-o-1/122367/2 "2024-11-07T05:45:32Z")

</div>

this is a performance bug in 1.11. the fix has been merged into 1.12 already and I think it is getting backported.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [November 7, 2024, 5:46am UTC](https://discourse.julialang.org/t/is-empty-o-1/122367/3 "2024-11-07T05:46:12Z")

</div>

see [make `_unsetindex` fast for isbits eltype by oscardssmith · Pull Request #56364 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/56364)

---

<div class="post-metadata">

### Author: ![guoyongzhi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guoyongzhi/32/28306_2.png) [@guoyongzhi](https://discourse.julialang.org/u/guoyongzhi)
#### Post date: [November 7, 2024, 5:54am UTC](https://discourse.julialang.org/t/is-empty-o-1/122367/4 "2024-11-07T05:54:29Z")

</div>

Okay… I never thought it would be a bug… This once led me into self-doubt.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [November 7, 2024, 12:35pm UTC](https://discourse.julialang.org/t/is-empty-o-1/122367/5 "2024-11-07T12:35:49Z")

</div>

Trust your reasoning, especially if you have hard data that supports it. 🙂
