[Numpy-discussion] Double-ended queues

Sturla Molden sturla@molden...
Tue Sep 25 06:31:59 CDT 2012


On 25.09.2012 11:38, Nathaniel Smith wrote:

> Implementing a ring buffer on top of ndarray would be pretty
> straightforward and probably work better than a linked-list
> implementation.

Amazingly, many do not know that a ringbuffer is simply an array indexed 
modulus its length:

foo = np.zeros(n)
i = 0
while 1:
   foo[i % n]  # access ringbuffer
   i += 1

Also, instead of writing a linked list, consider collections.deque.
A deque is by definition a double-ended queue. It is just waste of time 
to implement a deque (double-ended queue) and hope it will perform 
better than Python's standard lib collections.deque object.


Sturla




More information about the NumPy-Discussion mailing list