# I/O function takes far too long

//parses the text path vector into the engine
void Level::PopulatePathVectors(string pathTable)
{
// Read the file line by line.
ifstream myFile(pathTable);

for (unsigned int i = 0; i < nodes.size(); i++)
{
pathLookupVectors.push_back(vector<vector<int>>());

for (unsigned int j = 0; j < nodes.size(); j++)
{
string line;

if (getline(myFile, line)) //enter if a line is read successfully
{
stringstream ss(line);
istream_iterator<int> begin(ss), end;
pathLookupVectors[i].push_back(vector<int>(begin, end));
}
}
}
myFile.close();
}


This function is taking about 5 minutes to pass in a text file which is 744 * 744 lines long. Each line is a list of integers like this:

1 4 6 24 7 4 8 n


they can be of varying length. I have no idea why it's taking so long, but it needs to run in a matter of seconds, not minutes! The odd thing is that the same function in C# (in fact a very unoptimised version) does in fact only take seconds to run.

Could the problem be a setting?

This is a link to the path table it is parsing http://dl.dropbox.com/u/13519335/WepTestLevelPathTable.txt

• what is the type of nodes, it seems like, if nothing else, you could at least evaluate that once outside the double loop. I don't suspect that is the culprit, just mentioning it. – pstrjds Nov 8 '11 at 14:11
• First this is not C++ code this is C++11. (It does not compile as C++03) So I'll change the tag. So I presume you are trying to get the move semantics of vector to work correctly to prevent copying. – Martin York Nov 8 '11 at 14:42
• Well I don;t know what you are doing. But with node.size() = 744 It only takes 3 seconds to load the file for me. So this looks like more of a problem for SO than here. – Martin York Nov 8 '11 at 14:59

I setup nodes.size() to return 744.
This then forces all 553536 to be read from the file.

When I run your code it completes in 3.8 seconds (with -O3 it takes 2.5 seconds).

If I update the code to reserve the appropriate amount of space in each vector we can reduce the time it takes to: 3.3 seconds (with -O3 2.2 seconds).

So for the 5 minute figure you are quoting there must be some other error in your code.

Pass no-mutable parameters by cost reference (to avoid the copy).

void Level::PopulatePathVectors(string pathTable)


Add lines to reserve the appropriate space in each vector. This will prevent multiple re-allocations of the vector while you read. This is important when you have a triply nested vector.

    pathLookupVectors.push_back(vector<vector<int> >());

myFile.close();