[英]Faster lookup than std::set
I need a faster membership lookup for some legacy packet processing code which needs to identify if a packet with a particular ID is in a particular list. 我需要对一些遗留数据包处理代码进行更快的成员资格查找,这需要识别具有特定ID的数据包是否在特定列表中。
The list is only updated every few seconds while the packet matching happens very very often, so lookup performance is more important than insertion/deletion etc. 该列表仅每隔几秒更新一次,而数据包匹配经常发生,因此查找性能比插入/删除等更重要。
General Flow: 一般流程:
forall(special_PacketIDs)
{
pktIdSet.insert(theSpecialPktId)
}
while (1)
{
pkt = readPkt();
pktID = getPktIdOfPkt(pkt);
if ( aSpecialPkt(pktID) )
doSomething();
}
And right now, aSpecialPkt(pktId)
is defined as: 而现在,
aSpecialPkt(pktId)
定义为:
bool PktProcessor::aSpecialPkt(unsigned short pid)
{
return pktPidSet.find(pid) != pktPidSet.end();
}
gprof reports a lot of time spent in the std::set::find() gprof报告了在std :: set :: find()中花费的大量时间
The range of pktId is only 8192 possible values. pktId的范围仅为8192个可能的值。 Allocate a linear array would be much faster at the expense of memory, something like:
以内存为代价分配线性阵列会更快,例如:
class LinearSet
{
public:
void insert(pid) { mPktIdSet[pid] = true; }
bool elementExists(pid) { return mPktIdSet[pid]; }
private:
bool mPktIdSet[8192];
}
My question is whether there is a more "C++" way of doing this while maintaining top performance? 我的问题是,在保持最佳性能的同时,是否有更多的“C ++”方法可以做到这一点?
如果您知道有8192种可能性,那么您最好的选择可能是std::bitset<8192>
,它将使用千字节并且非常适合缓存。
std::bitset<8192>
is a good choice, but it really depends on your platform as well as the number of special packet IDs. std::bitset<8192>
是一个不错的选择,但它实际上取决于您的平台以及特殊数据包ID的数量。 See this question: Choosing between set<int> vs. vector<bool> vs. vector<boolean_t> to use as a bitmap (bitset / bit array) 看到这个问题: 在set <int>与vector <bool>和vector <boolean_t>之间选择用作位图(bitset / bit array)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.