小编Par*_*thN的帖子

优化C++代码(使用UnorderedMap和Vector)

我正在尝试优化需要很长时间的C++代码的某些部分(对于X数据量,代码的以下部分需要大约19秒,并且我试图在不到5秒的时间内完成整个过程相同数量的数据 - 基于我的一些基准测试.我有一个函数"add",我已经编写并复制了代码.我将尝试尽可能多地解释我认为需要理解代码.如果我错过了什么,请告诉我.

对于X数据条目,以下函数add被称为X次.

void HashTable::add(PointObject vector)   // PointObject is a user-defined object
{
    int combinedHash = hash(vector);   // the function "hash" takes less than 1 second for X amount of data

   // hashTableMap is an unordered_map<int, std::vector<PointObject>>

   if (hashTableMap.count(combinedHash) == 0)
   {
        // if the hashmap does not contain the combinedHash key, then 
        //  add the key and a new vector
        std::vector<PointObject> pointVectorList;
        pointVectorList.push_back(vector);
        hashTableMap.insert(std::make_pair(combinedHash, pointVectorList));
   }
   else
   {
        // otherwise find the key and the corresponding vector of …
Run Code Online (Sandbox Code Playgroud)

c++ optimization unordered-map vector

11
推荐指数
2
解决办法
874
查看次数

标签 统计

c++ ×1

optimization ×1

unordered-map ×1

vector ×1