高并发访问的时代,我们要得到商品或评论的Top 10 ,如果需要去通过查询数据库,那么显然是效率低下的。一个解决方案是在内存中存放一个固定长度的数组,每当新数据从上面压入, 就有相应数量的旧数据就从下面被顶出
常规办法:利用numpy的Array构建动态数组
import numpy as np
class DynamicArray(object):
"""基于np扩展一个固定长度的动态数组"""
def __init__(self, length, name = None, item_type = float):
self.name = name
self._data = np.zeros(length, dtype=item_type)
self._length = length
self._dataSize = 0
def append(self, value):
"""动态添加数据"""
self._data[0:self._length - 1] = self._data[1:self._length]
self._data[-1] = value
self._dataSize += 1
self._dataSize = min(self._dataSize, self._length)
def getData(self):
"""获取数据"""
return self._data
其实,我们可以采用Python内建的一个集合模块
Deques是堆栈和队列的泛化(名称发音为“deck”,是“双端队列”的缩写)。 Deques支持线程安全,高效的内存追加,并从双侧出现,并且在任一方向都具有大致相同的O(1) 性能。
虽然列表 list
对象也支持类似的操作,但是Deques针对快速固定长度操作进行了优化,并且会导致pop(0)
和 insert(0, v)
操作的O(n)内存移动成本,这会改变底层数据表示的大小和位置。如果没有指定maxlen或者是None,deques可能会增长到任意长度。否则,deque被限制到指定的最大长度。
一旦固定长度的deque已满,当添加新项目时,相应数量的项目将从相反的一端被丢弃。有界长度deques提供类似于Unix中尾部tail
过滤器的功能。它们也可用于跟踪只有最近的活动感兴趣的数据池。 Deque对象支持以下方法:
append(x)
Add x to the right side of the deque.
appendleft(x)
Add x to the left side of the deque.
clear()
Remove all elements from the deque leaving it with length 0.
copy()
Create a shallow copy of the deque.
New in version 3.5.
count(x)
Count the number of deque elements equal to x.
New in version 3.2.
extend(iterable)
Extend the right side of the deque by appending elements from the iterable argument.
extendleft(iterable)
Extend the left side of the deque by appending elements from iterable. Note, the series of left appends results in reversing the order of elements in the iterable argument.
index(x[, start[, stop]])
Return the position of x in the deque (at or after index start and before index stop). Returns the first match or raises ValueError if not found.
New in version 3.5.
insert(i, x)
Insert x into the deque at position i.
If the insertion would cause a bounded deque to grow beyond maxlen, an IndexError is raised.
New in version 3.5.
pop()
Remove and return an element from the right side of the deque. If no elements are present, raises an IndexError.
popleft()
Remove and return an element from the left side of the deque. If no elements are present, raises an IndexError.
remove(value)
Remove the first occurrence of value. If not found, raises a ValueError.
reverse()
Reverse the elements of the deque in-place and then return None.
New in version 3.2.
rotate(n=1)
Rotate the deque n steps to the right. If n is negative, rotate to the left.
When the deque is not empty, rotating one step to the right is equivalent to d.appendleft(d.pop()), and rotating one step to the left is equivalent to d.append(d.popleft()).
Deque objects also provide one read-only attribute:
maxlen
Maximum size of a deque or None if unbounded.
New in version 3.1.
网友评论