使用多线程的无序映射的 C++ 填充
C++ population of unordered map using multithreading
我想知道是否有一种方法可以加快名为 (path_from_startend) 的无序地图的填充速度。无序映射总是有一个唯一的键。
#pragma omp parallel for
for (int i=start_index;i<end_index;++i){
for (int j=start_index;j<end_index;++j){
string key= to_string(unique_locations[i])+to_string(unique_locations[j]);
//dont compute path to itself
if (i==j){
}
else {
vector <unsigned> path = pathbot.FindPath(unique_locations[i],unique_locations[j],turn_penalty);
path_from_startend.insert(make_pair(key,path));
}
}
}
您可以试试下面的模式是否能提高您的速度。您基本上是在填充部分地图,然后将其合并到整个地图中。加速在很大程度上取决于构建和插入元素所花费的时间。如果您的元素构造和插入的成本很低,那么您的加速甚至可能是负数,因为对于每个部分地图,它必须遍历整个地图以搜索重复项。
#pragma omp parallel
{
std::unordered_map < std::string, std::vector < unsigned > > partial;
#pragma omp for
for (int i = start_index; i < end_index; ++i)
{
for (int j = start_index; j < end_index; ++j)
{
std::string key = std::to_string(unique_locations[i])
+ std::to_string(unique_locations[j]);
//dont compute path to itself
if (i != j)
{
std::vector<unsigned> path = pathbot.FindPath(unique_locations[i],
unique_locations[j],
turn_penalty);
partial.insert(std::make_pair(key,path));
}
}
}
#pragma omp critical
path_from_startend.insert(partial.begin(),partial.end());
}
我想知道是否有一种方法可以加快名为 (path_from_startend) 的无序地图的填充速度。无序映射总是有一个唯一的键。
#pragma omp parallel for
for (int i=start_index;i<end_index;++i){
for (int j=start_index;j<end_index;++j){
string key= to_string(unique_locations[i])+to_string(unique_locations[j]);
//dont compute path to itself
if (i==j){
}
else {
vector <unsigned> path = pathbot.FindPath(unique_locations[i],unique_locations[j],turn_penalty);
path_from_startend.insert(make_pair(key,path));
}
}
}
您可以试试下面的模式是否能提高您的速度。您基本上是在填充部分地图,然后将其合并到整个地图中。加速在很大程度上取决于构建和插入元素所花费的时间。如果您的元素构造和插入的成本很低,那么您的加速甚至可能是负数,因为对于每个部分地图,它必须遍历整个地图以搜索重复项。
#pragma omp parallel
{
std::unordered_map < std::string, std::vector < unsigned > > partial;
#pragma omp for
for (int i = start_index; i < end_index; ++i)
{
for (int j = start_index; j < end_index; ++j)
{
std::string key = std::to_string(unique_locations[i])
+ std::to_string(unique_locations[j]);
//dont compute path to itself
if (i != j)
{
std::vector<unsigned> path = pathbot.FindPath(unique_locations[i],
unique_locations[j],
turn_penalty);
partial.insert(std::make_pair(key,path));
}
}
}
#pragma omp critical
path_from_startend.insert(partial.begin(),partial.end());
}