Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple, efficient FIFO queue with Elixir

Tags:

elixir

Is there a built-in datatype that I can use for a FIFO queue in Elixir?

I have looked at list, but it seems like it is not efficient to get the list tail? Maybe I am wrong about this?

My queue needs to hold a 100,000 or more elements. My need is that I am generating 44100 tuples a second of the form {t, y} for audio. I am trying to implement a delay, so I need to queue up a couple of seconds (maybe 10 at max?) of audio before sending it down the processing pipeline.

like image 893
Josh Petitt Avatar asked Jan 30 '16 14:01

Josh Petitt


1 Answers

I'm not sure about the performance on 100,000 or more elements but it looks like you're looking for Queue. It's a FIFO Queue provided by the Erlang/OTP.

Example:

iex(39)> queue = :queue.new                            
{[], []}
iex(40)> queue = :queue.in(1, queue)                   
{[1], []}
iex(41)> queue = :queue.in(2, queue)                   
{[2], [1]}
iex(42)> queue = :queue.in(3, queue)
{[3, 2], [1]}
iex(43)> :queue.out(queue)                             
{{:value, 1}, {[3], [2]}}
iex(44)> {{:value, head}, queue} = :queue.out(queue)
{{:value, 1}, {[3], [2]}}
iex(45)> head
1
iex(46)> {{:value, head}, queue} = :queue.out(queue)
{{:value, 2}, {[], [3]}}
iex(47)> head
2
iex(48)> {{:value, head}, queue} = :queue.out(queue)
{{:value, 3}, {[], []}}
iex(49)> head
3
iex(51)> :queue.out(queue)        
{:empty, {[], []}}
like image 62
arathunku Avatar answered Oct 22 '22 19:10

arathunku