固定大小优先级队列 - 先插入还是先删除?
我使用 priority_queue
来存储迄今为止在 K 最近邻搜索中找到的 K 个最近点。当我找到一个比队列顶部的点更近的点时,我想弹出顶部元素并推送新元素。
if(point < pq.top()){
pq.pop();
pq.push(point);
}
一般是先pop后insert效率更高,还是先insert再pop效率更高?
I am using a priority_queue
to store the K closest points found so far in a K-nearest-neighbor search. When I find a point closer than the point at the top of the queue, I want to pop the top element and push the new one.
if(point < pq.top()){
pq.pop();
pq.push(point);
}
Generally, is it more efficient to pop first and then insert, or is it more efficient to insert first and then pop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
std::priority_queue
作为优先级队列类,则默认情况下,标准容器类std::vector
将用作其基础容器类。一般来说,先
push
的效率比先pop
的效率低。原因一
将元素推送到
priority_queue
中将调用vector::push_back
,如果底层缓冲区超出其当前容量,它可能会重新分配底层缓冲区。原因二
假设优先级队列中现在有N 个元素。
如果您先
push
,算法push_heap
会被调用两次,以调整N+1和N+1元素,分别。如果先
pop
,算法push_heap
会被调用两次,分别调整N和N个元素。旁白
如果您要实现自己的优先级队列,这可能会节省性能。由于您已经检查了顶部的值,我想知道您是否可以直接将元素与顶部交换,而不调用推送/弹出,从而绕过堆调整算法。但可能不实用。
If you are using
std::priority_queue
as your priority queue class, the standard container classstd::vector
is used for its underlying container class, by default.Generally, it is less efficient to
push
first thanpop
first.Reason one
Pushing an element in
priority_queue
will envokevector::push_back
which can potentially reallocate the underlying buffer if it exceeds it current capacity.Reason two
Assume there are now N elements in priority queue.
If you
push
first, the algorithmpush_heap
is called two times, to adjust N+1 and N+1 elements, respectively.If you
pop
first, the algorithmpush_heap
is called two times, to adjust N and N elements, respectively.Aside
If you're implementing your own priority queue, this is probably a performance-saver. Since you already check the value with the top, I'm wondering if you can directly swap the element with the top without invoking the push/pop thus bypassing the heap adjusting algorithm. May not be practical though.
@NullSet,
您正在实现 K 最近邻搜索,所以我假设性能是一个大问题。
如果是这样,只需使用标准队列的一个技巧,看看是否可以用数组支持它(我在这里超出了自己的深度)...我猜这个是固定的-大小,随机访问构造将比向量更有效。
然后,如果您的队列仍然是一个经过验证的性能瓶颈,我会考虑基于 btree(甚至 rbtree)滚动我自己的优先级队列接口实现。
你能走多远实际上取决于你的最大K。如果K足够小,标准向量支持的优先级队列将是最有效的可想到的解决方案。诀窍是观察正在运行的程序的实际性能,以便识别那些可能为您的努力带来最佳性能改进的“改进机会”。
是的,我是算法赛车迷……这表明了吗?
干杯。基思.
@NullSet,
You're implementing a K-nearest-neighbor-search, so I'm going to presume that performance is a BIG concern.
If so, Just one tip with using the standard-queue, see if you can back it with an array (I'm out of my own depth here)... I'm guessing this fixed-size, random-access construct will be a tad more efficient that vector.
Then, if your queue is STILL a proven performance bottleneck, I'd look at rolling-my-own implementation of the priority-queue interface based on a btree (or even an rbtree).
How far you go with this is REALLY dependant on your maximum K. If K is small enough the standard vector-backed priority queue will be darn-near-as-quick-as THE most efficient conceivable solution. The trick is to observe the ACTUAL performance of the running program, in order to identify those "opportunities for improvement" which are likely to yeild the best performance improvement for your efforts.
Yup, I'm an algorithm racing fan... does it show?
Cheers. Keith.