gpt4 book ai didi

c++ - 速度差 : separate functor VS operator() inside a big class with *this

转载 作者:太空宇宙 更新时间:2023-11-03 10:36:56 24 4
gpt4 key购买 nike

我正在使用 C++ STL 堆算法,并且围绕它编写了一个包装器类,这样我就可以做一些其他的事情。例如,当我尝试使用下面的代码时:

//! Min-heap wrapper class.
class FMMHeap{
public:
FMMHeap(Vector &phi) : _phi(phi) {}
bool operator()(unsigned p1, unsigned p2) {return fabs(_phi(p1)) > fabs(_phi(p2)); }
inline void pop(){ pop_heap(_heap.begin(),_heap.end(),*this); _heap.pop_back(); }
[...lots of other stuff...]
vectorU32 _heap;
Vector &_phi;
}

这比我有一个像这样的单独函数对象时慢了 wayyyyy:

struct HeapSort{
public:
HeapSort(Vector &phi) : _phi(phi) {}
bool operator()(unsigned p1, unsigned p2) {return fabs(_phi(p1)) > fabs(_phi(p2)); }
private:
Vector &_phi;
};

class FMMHeap{
public:
FMMHeap(Vector &phi) : cmp(phi) {}
inline void pop(){ pop_heap(_heap.begin(),_heap.end(),cmp); _heap.pop_back(); }
[...lots of other stuff...]
vectorU32 _heap;
HeapSort cmp;
}

我不确定这是为什么。减速是否来自 *this 因为该类有大量数据?这似乎很奇怪。还是与函数对象的使用方式有关?

最佳答案

我不确定:但也许 pop_heap 最终会复制您传入的仿函数对象。FMMHeap 的拷贝比简单的 HeapSort

更昂贵

关于c++ - 速度差 : separate functor VS operator() inside a big class with *this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1720712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com