HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

I/O function takes far too long

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
longfunctiontoofartakes

Problem

//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 >());

            for (unsigned int j = 0; j  begin(ss), end;
                    pathLookupVectors[i].push_back(vector(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

Solution

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.

Comments on 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 >());
    pathLookupVectors.reserve(nodes.size());                // Add this line

    for (unsigned int i = 0; i >());
        pathLookupVectors.back().reserve(nodes.size())      // Add this line


No Need to manually close the file.

see https://stackoverflow.com/q/748014/14065

Implementation using fstream failed evaluation

myFile.close();

Code Snippets

void Level::PopulatePathVectors(string pathTable)
pathLookupVectors.push_back(vector<vector<int> >());
    pathLookupVectors.reserve(nodes.size());                // Add this line

    for (unsigned int i = 0; i < nodes.size(); i++)
    {
        pathLookupVectors.push_back(vector<vector<int>>());
        pathLookupVectors.back().reserve(nodes.size())      // Add this line
myFile.close();

Context

StackExchange Code Review Q#5892, answer score: 5

Revisions (0)

No revisions yet.