- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我一直在尝试在 Python 中优化 BFS 实现的性能,我最初的实现是使用 deque 来存储要扩展的节点队列和 dict 来存储相同的节点,这样我就可以有效地查找它是否已经打开了。
我尝试通过迁移到 OrderedDict 来优化(简单性和效率)。然而,这需要更多的时间。使用 deque/dict 完成 400 个样本搜索需要 2 秒,而仅使用 OrderedDict 则需要 3.5 秒。
我的问题是,如果 OrderedDict 的功能与两个原始数据结构相同,那么它至少在性能上不应该相似吗?或者我在这里错过了什么?下面的代码示例。
仅使用 OrderedDict:
open_nodes = OrderedDict()
closed_nodes = {}
current = Node(start_position, None, 0)
open_nodes[current.position] = current
while open_nodes:
current = open_nodes.popitem(False)[1]
closed_nodes[current.position] = (current)
if goal(current.position):
return trace_path(current, open_nodes, closed_nodes)
# Nodes bordering current
for neighbor in self.environment.neighbors[current.position]:
new_node = Node(neighbor, current, current.depth + 1)
open_nodes[new_node.position] = new_node
同时使用双端队列和字典:
open_queue = deque()
open_nodes = {}
closed_nodes = {}
current = Node(start_position, None, 0)
open_queue.append(current)
open_nodes[current.position] = current
while open_queue:
current = open_queue.popleft()
del open_nodes[current.position]
closed_nodes[current.position] = (current)
if goal_function(current.position):
return trace_path(current, open_nodes, closed_nodes)
# Nodes bordering current
for neighbor in self.environment.neighbors[current.position]:
new_node = Node(neighbor, current, current.depth + 1)
open_queue.append(new_node)
open_nodes[new_node.position] = new_node
最佳答案
deque 和 dict 都是用 C 实现的,运行速度比用纯 Python 实现的 OrderedDict 快。
OrderedDict 的优点是它具有 O(1) 的 getitem、setitem 和 delitem,就像常规的 dicts 一样。这意味着它可以很好地扩展,尽管纯 python 实现速度较慢。
使用双端队列、列表或二叉树的竞争实现通常会在其中一个类别中放弃快速的 big-Oh 时间,以便在另一个类别中获得速度或空间优势。
更新:从 Python 3.5 开始,OrderedDict() 现在具有 C 实现。尽管它没有像其他一些容器那样经过高度优化。它的运行速度应该比纯 python 实现快很多。然后从 Python 3.6 开始,对常规字典进行了排序(尽管还不能保证排序行为)。那些应该跑得更快:-)
关于python - OrderedDict 性能(与 deque 相比),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8176513/
这个问题在这里已经有了答案: Is using std::deque or std::priority_queue thread-safe? [duplicate] (3 个答案) Thread s
我是一名学生,我的操作系统类(class)项目有一点问题,这对于作业规范本身来说有点多余: 虽然我可以将 100 万个双端队列推送到我的双端队列中,但我无法推送约 1000 万或更多。 现在,在实际的
std::deque 在 CppReference 中有很好的记录,但是 boost::deque 的 documentation看起来和标准的一样,只是增加了一些方法,比如nth和index_of。
Scala 是否具有类似于 Java Deque 或 Python deque 的双面队列? 我在 Scala 2.12 API 中只看到 Stack 和 Queue 但只想仔细检查一下。 最佳答案
好的,我正在实现一个与 Apple 的日历应用程序非常相似的日历。我的 UICollectionView 中有多种类型的单元格。我有垂直线单元格、水平线单元格、现在线单元格和事件单元格。 每当我滚动时
目前在我的项目中我有两个静态方法PushObjects 和ProcessObject。 PushObject 方法将数据推回静态双端队列,此方法可由多个线程访问,但 ProcessObject 始终由
使用 http://www.cppreference.com/wiki/stl/deque/insert作为引用,我在某些位置将值插入双端队列。 例如,如果双端队列 A 是: a, b, d, e,
Python 的 collections.deque有一个 maxlen 参数,这样 [...] the deque is bounded to the specified maximum lengt
这是我的第一个问题,希望大家都好。我必须编写双端队列或双端队列的数组实现,但在理解前面方法的入队元素时遇到了一些麻烦,我通过摆弄了一下让它工作,但我仍然很难理解逻辑: void addAtFront(
我试图解决 Java Deque 上的 HackerRank 问题。除了具有 100,000 个输入的案例之外,我的代码通过了所有案例。 问题:在这个问题中,给你 N 个整数。您需要在大小为 M 的所
我正在编写 Deque 以便能够从 Front 和 Rear 中添加和删除....我认为我的逻辑是错误的,但我无法弄清楚!因为当我从前面插入时,它必须从后面移除,但也从前面移除。 你会检查我的代码并帮
我在使用 deque 容器时有些困惑。 我将 vector 与 deque 进行了比较,我动态地输入了 Integer 值并观察到在几次插入之后 vector 的对象开始四处移动并且地址已被更改,这似
由于我对内存有严格的要求,因此在从双端队列的开头删除一个范围后,我尝试使用deque::shrink_to_fit。但是,它不起作用,我只看到libstdc++使用带有副本的swap技巧实现了shri
我需要为继承到 deque 的类添加功能,但更喜欢看到 collections.deque 中的代码来实现一个新类 final。 >>> from _collections import deque,
本文实例讲述了python3 deque 双向队列创建与使用方法。分享给大家供大家参考,具体如下: 创建双向队列 ?
考虑以下 C++ 程序: #include #include using namespace std; int main() { deque d(30000000); cout
尝试使用 Deque 数据结构来回答一个编程问题,以查找乘积小于目标的所有子数组。 如上所述,我想使用 Deque 数据结构。我查看了用法,认为我做对了,但是使用了 const Deque = req
双端队列实现我实现了一个通用的 Deque 数据结构。 请检查此实现 这个错误对我来说没有意义,请告诉我一点信息 import java.util.NoSuchElementException; im
我需要编写自己的 Deque 类,并且必须使用双向链表实现来存储数据。问题是编写方法pushfromLeft(Thing thing),它将插入到双端队列的左侧。以下是我迄今为止所拥有的,但似乎不起作
标准说: A deque is a sequence container that supports random access iterators (27.2.7). In addition, it
我是一名优秀的程序员,十分优秀!