高效的循环缓冲区? [英] efficient circular buffer?
问题描述
我想在 python 中创建一个高效的循环缓冲区(目的是取整数值的平均值)缓冲).
这是使用列表收集值的有效方法吗?
def add_to_buffer( self, num ):self.mylist.pop( 0 )self.mylist.append( num )
什么会更有效(以及为什么)?
我会使用 collections.deque
带有 maxlen
arg
deque 的文档中有一个 recipe
与您想要的类似.我断言它是最有效的完全基于这样一个事实,即它是由非常熟练的团队用 C 实现的,他们习惯于编写一流的代码.
I want to create an efficient circular buffer in python (with the goal of taking averages of the integer values in the buffer).
Is this an efficient way to use a list to collect values?
def add_to_buffer( self, num ):
self.mylist.pop( 0 )
self.mylist.append( num )
What would be more efficient (and why)?
I would use collections.deque
with a maxlen
arg
>>> import collections
>>> d = collections.deque(maxlen=10)
>>> d
deque([], maxlen=10)
>>> for i in xrange(20):
... d.append(i)
...
>>> d
deque([10, 11, 12, 13, 14, 15, 16, 17, 18, 19], maxlen=10)
There is a recipe in the docs for deque
that is similar to what you want. My assertion that it's the most efficient rests entirely on the fact that it's implemented in C by an incredibly skilled crew that is in the habit of cranking out top notch code.
这篇关于高效的循环缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!