我正在实现数学函数,因此需要有一个优先级队列。我使用此页上的以下代码:
class MyPriorityQueue(PriorityQueue):
def __init__(self):
PriorityQueue.__init__(self)
self.counter = 0
def put(self, item, priority):
PriorityQueue.put(self, (priority, self.counter, item))
self.counter += 1
def get(self, *args, **kwargs):
if self.counter == 0:
return None
_, _, item = PriorityQueue.get(self, *args, **kwargs)
self.counter -= 1
return item
def empty(self):
if self.counter == 0:
return True
return False
众所周知python速度很慢,但是看到结果我意识到出队消耗了总执行时间的28%。任何人有任何建议吗?
Line # Hits Time Per Hit % Time Line Contents
==============================================================
34 @profile
35 def solution(self):
36
37 1 11 11.0 0.0 root = Node()
38 1 2 2.0 0.0 root.capacity = self.K - root.size
39 1 65 65.0 0.0 root.estimated = self.bound(root.level, root.size, root.value)
40 1 4 4.0 0.0 root.copyList(None)
41 1 37 37.0 0.0 self.queue.put(root, -0)
42
43 99439 389936 3.9 2.3 while not self.queue.empty():
44
45 99438 4666742 46.9 28.0 node = self.queue.get()
46
47 99438 272335 2.7 1.6 if node.estimated > self.maxValue:
48
慕尼黑的夜晚无繁华
动漫人物
相关分类