Reputation: 229
Trying to understand the cause of the performance difference. I'm reading a ~70M file with the function below. Running the code with: gcc 4.4.6 takes less than a second gcc 3.2.3 takes more than 6 minutes
Most of the time is spend in the assign part. What was changed to make this speed difference between the 2 compilers ?
bool ReadFile(const string& path, string& file_data)
{
ifstream ifs(path.c_str(), ifstream::in | ifstream::ate);
if (!ifs) return false;
int size = ifs.tellg();
if (size==0) return false;
ifs.seekg(0, ios::beg);
file_data.assign((istreambuf_iterator<char>(ifs)),
istreambuf_iterator<char>());
return true;
}
Upvotes: 4
Views: 255
Reputation: 38209
Could you try tweak a bit this code (one extra line):
bool ReadFile(const string& path, string& file_data)
{
ifstream ifs(path.c_str(), ifstream::in | ifstream::ate);
if (!ifs) return false;
int size = ifs.tellg();
if (size==0) return false;
ifs.seekg(0, ios::beg);
file_data.reserve(size);
file_data.assign((istreambuf_iterator<char>(ifs)),
istreambuf_iterator<char>());
return true;
}
and do measurement again
In second attempt you can try this:
bool ReadFile(const string& path, string& file_data)
{
ifstream ifs(path.c_str(), ifstream::in | ifstream::ate);
if (!ifs) return false;
int size = ifs.tellg();
if (size==0) return false;
ifs.seekg(0, ios::beg);
file_data.resize(size);
return ifs.read(&file_data[0], size);
}
Upvotes: 3