在python中实现高效的固定大小FIFO



我需要在python或numpy中高效地实现固定大小的FIFO。我可能有不同的FIFO,有些用于整数,有些用于字符串,等等。在这个FIFO中,我需要通过索引访问每个元素。

对效率的关注是因为这些FIFO将被用于计划的核心,该计划预计将连续运行几天,并且预计大量数据将通过它们。因此,该算法不仅需要时间效率,还必须具有内存效率。

现在,在C或Java等其他语言中,我将使用循环缓冲区和字符串指针(用于字符串FIFO(来有效地实现这一点。在python/numpy中,这是一种有效的方法,还是有更好的解决方案?

具体来说,这些解决方案中哪一个最有效:

(1( 设置了maxlen值的出队:(垃圾收集对出队效率的影响是什么?(

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( 列表子类解决方案(取自Python,将列表强制为固定大小(:

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( 一个普通的numpy数组。但是这限制了字符串的大小,那么如何指定最大字符串大小呢?

a = np.array(['apples', 'foobar', 'cowboy'])
a[2] = 'bananadgege'
print(a)
> ['apples' 'foobar' 'banana']
# now add logic for manipulating circular buffer indices

(4( 上面的对象版本,但任意长度字符串的python numpy数组表示,使用对象会抵消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( 还是有比上面提到的更有效的解决方案?

顺便说一句,如果有帮助的话,我的字符串的长度有一个最大上限。。

我会使用NumPy。要指定最大字符串长度,请使用类似以下的dtype

np.zeros(128, (str, 32)) # 128 strings of up to 32 characters

最新更新