# Erlang :queue vs List

**URL:** <https://forum.elixirforum.com/t/erlang-queue-vs-list/20370>\
**Category:** Questions\
**Tags:** queues\
**Created:** [February 22, 2019, 8:18pm UTC](https://forum.elixirforum.com/t/erlang-queue-vs-list/20370 "2019-02-22T20:18:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alvises](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/alvises/32/2247_2.png) [@alvises](https://forum.elixirforum.com/u/alvises)\
**Post date:** [February 22, 2019, 8:18pm UTC](https://forum.elixirforum.com/t/erlang-queue-vs-list/20370/1 "2019-02-22T20:18:53Z")

</div>

After benchmarking [queues](http://erlang.org/doc/man/queue.html) they seem much faster than Lists on  
**appending**.

- `[1,...1000] ++ [1001]`**O(n)**
- `:queue.in(1,q)` should be **O(1)** right?

**last element**

- `List.last([1,..,1000])` again **O(n)**
- `:queue.get_r(q)` again **O(1)**

and they should be similar on **prepending**

- List `[0 | [1,..,1000]]`
- `:queue.in_r(0,q)`

Now, seeing queues implementation it seems reasonable. A queue generated from a list of numbers from 1 to 1000 is implemented as a tuple of two lists.

`{ [1000, 999, 998,.., 501], [1, 2, 3, 4, .., 500] }`

So the list is split in two and the last half part of the list is reversed. Since prepending an item in a list is fast O(1) then appending `1001` is fast because `1001` is prepended to the first list in the tuple. Same thing for getting the last element, with lists is O(n) and while with `:queue` is O(1).

My question is, what are the downsides of using queues over lists? Maybe enumerating the numbers from 500 to 1000 since that list is reversed?  
Some rebalancing when enqueueing new elements?

---

<div class="post-metadata">

**Author:** ![al2o3cr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/32/3457_2.png) [@al2o3cr](https://forum.elixirforum.com/u/al2o3cr)\
**Post date:** [February 22, 2019, 9:03pm UTC](https://forum.elixirforum.com/t/erlang-queue-vs-list/20370/2 "2019-02-22T21:03:45Z")

</div>

Your intuition is correct - the documentation for `:queue` has a list of slow operations:

```elixir
All operations [have] an amortized O(1) running time, except
filter/2, join/2, len/1, member/2, split/2 that have O(n).

```

See also the paper referenced in those docs, [“Purely Functional Data Structures” by Chris Okasaki](https://www.cs.cmu.edu/~rwh/theses/okasaki.pdf) for detailed analysis.

---

<div class="post-metadata">

**Author:** ![alvises](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/alvises/32/2247_2.png) [@alvises](https://forum.elixirforum.com/u/alvises)\
**Post date:** [February 22, 2019, 9:28pm UTC](https://forum.elixirforum.com/t/erlang-queue-vs-list/20370/3 "2019-02-22T21:28:35Z")

</div>

Thanks a lot for the pdf! Super interesting!!

Isn’t len O(n) also on lists?

len/1 couldn’t it be made O(1) wrapping the queue around a struct, which has a :count field that is incremented/decremented at each operation?

---

<div class="post-metadata">

**Author:** ![dom](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dom/32/9313_2.png) [@dom](https://forum.elixirforum.com/u/dom)\
**Post date:** [February 22, 2019, 11:10pm UTC](https://forum.elixirforum.com/t/erlang-queue-vs-list/20370/4 "2019-02-22T23:10:42Z")

</div>

Yes, some queue implementations do just that:

[https://github.com/Qqwy/elixir\_okasaki/blob/master/lib/okasaki/implementations/amortized\_queue.ex#L9](https://github.com/Qqwy/elixir_okasaki/blob/master/lib/okasaki/implementations/amortized_queue.ex#L9)

[http://ucsd-progsys.github.io/liquidhaskell-tutorial/09-case-study-lazy-queues.html](http://ucsd-progsys.github.io/liquidhaskell-tutorial/09-case-study-lazy-queues.html)
