固定大小优先级队列 - 先插入还是先删除?

发布于 2024-11-04 11:14:44 字数 234 浏览 3 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁绊已千年 2024-11-11 11:14:44

如果您使用 std::priority_queue 作为优先级队列类,则默认情况下,标准容器类 std::vector 将用作其基础容器类。

一般来说,先push的效率比先pop的效率低。

原因一

将元素推送到 priority_queue 中将调用 vector::push_back,如果底层缓冲区超出其当前容量,它可能会重新分配底层缓冲区。

原因二

priority_queue::pop

当你从其中弹出一个元素时
priority_queue,它调用
pop_heap 保留堆的算法
priority_queues 的属性,然后
调用成员函数pop_back
底层容器对象的
删除该元素。

priority_queue::push

当你将一个元素推入
priority_queue,它调用成员
底层函数push_back
容器对象,然后调用
push_heap 保留堆的算法
priority_queues 的属性。

假设优先级队列中现在有N 个元素。

如果您先push,算法push_heap会被调用两次,以调整N+1N+1元素,分别。

如果先pop,算法push_heap会被调用两次,分别调整NN个元素。

旁白

如果您要实现自己的优先级队列,这可能会节省性能。由于您已经检查了顶部的值,我想知道您是否可以直接将元素与顶部交换,而不调用推送/弹出,从而绕过堆调整算法。但可能不实用。

If you are using std::priority_queue as your priority queue class, the standard container class std::vector is used for its underlying container class, by default.

Generally, it is less efficient to push first than pop first.

Reason one

Pushing an element in priority_queue will envoke vector::push_back which can potentially reallocate the underlying buffer if it exceeds it current capacity.

Reason two

priority_queue::pop

When you pop an element from
priority_queue, it calls the
pop_heap algorithm to keep the heap
property of priority_queues, and then
calls the member function pop_back
of the underlying container object to
remove the element.

priority_queue::push

When you push an element to
priority_queue, it calls the member
function push_back of the underlying
container object, and then calls the
push_heap algorithm to keep the heap
property of priority_queues.

Assume there are now N elements in priority queue.

If you push first, the algorithm push_heap is called two times, to adjust N+1 and N+1 elements, respectively.

If you pop first, the algorithm push_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.

终止放荡 2024-11-11 11:14:44

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文