I need to efficiently implement a fixed size FIFO in python or numpy. And I might have different such FIFOs, some for integers, some for strings, etc. In this FIFO, I will need to access each element by its index.
Concern for efficiency is because these FIFOs will be used at the core of a program that is expected to run for several days continuously, and large volume of data is expected to pass through them. So not only will the algorithm need to be time efficient, it also has to be memory efficient.
Now in other languages like C or Java, I would implement this efficiently using a circular buffer, and string pointers (for the string FIFOs). Is that the an efficient approach in python/numpy, or is there a better solution?
Specifically, which of these solutions is the most efficient:
(1) dequeue with maxlen value set: (what will be the effect of garbage collection on the dequeue's efficiency?)
import collections
l = collections.deque(maxlen=3)
l.append('apple'); l.append('banana'); l.append('carrot'); l.append('kiwi')
print(l, len(l), l[0], l[2])
> deque(['banana', 'carrot', 'kiwi'], maxlen=3) 3 banana kiwi
(2) list subclass solution (taken from Python, forcing a list to a fixed size):
class L(list):
def append(self, item):
list.append(self, item)
if len(self) > 3: self[:1]=[]
l2.append('apple'); l2.append('banana'); l2.append('carrot'); l2.append('kiwi')
print(l2, len(l2), l2[2], l2[0])
> ['banana', 'carrot', 'kiwi'] 3 kiwi banana
(3) a normal numpy array. But this limits the size of the strings, so how to specify the max string size for this?
a = np.array(['apples', 'foobar', 'cowboy'])
a[2] = 'bananadgege'
print(a)
> ['apples' 'foobar' 'banana']
# now add logic for manipulating circular buffer indices
(4) an object version of above, but python numpy array of arbitrary length strings says that using an object undoes the benefits of numpy
a = np.array(['apples', 'foobar', 'cowboy'], dtype=object)
a[2] = 'bananadgege'
print(a)
> ['apples' 'foobar' 'bananadgege']
# now add logic for manipulating circular buffer indices
(5) or is there a more efficient solution than the ones presented above?
BTW, my strings have a max upper bound in their length, if that is of any help..