jeudi 23 janvier 2020

Why accessing map values by using at in C++ is so slow when key values are std vectors?

I'm using std::map defined as std::map<std::vector<int>, double> and you see the key values are vector of integers. The number of members in my map is 24600. I initialize my map like this:

const std::string in_name = velocityWeightsFilePath;

std::map<std::vector<int>, double> weights_table;

/* Load and read file. */
std::fstream myfile;
myfile.open(in_name.c_str(), std::ios_base::in);

std::string input_line;
/* input files are in ASCII, in format:
 *
 * coord_x coord_y coord_z weights_value
 *
 * */
while (myfile.good())
{
            int x, y, z;
            double v;
            myfile >> x >> y >> z >> v;

            std::vector<int> xyz;
            xyz.push_back(x);
            xyz.push_back(y);
            xyz.push_back(z);

            weights_table[xyz] = v;
}
myfile.close();

Then later, I access this map like this in this function:

double GetValue(std::vector<int> xyz) const
{
      double value;

      value = weights_table.at(xyz);

      return value;
}

I call GetValue function exactly 24600 times to lookup for my values, but it's so slow that even after 4 or 5 hours I didn't even get one of my values by using GetValue function and also I don't get any exception. Basically, it just hangs before this line value = weights_table.at(xyz);. By the way, I use Intel 18.0 with -O3 optimization. Is something wrong here or is there any better approach here?

Aucun commentaire:

Enregistrer un commentaire