[英]Same code huge performance difference in different gcc compilers
Trying to understand the cause of the performance difference. 试图了解性能差异的原因。 I'm reading a ~70M file with the function below.
我正在阅读带有以下功能的~70M文件。 Running the code with: gcc 4.4.6 takes less than a second gcc 3.2.3 takes more than 6 minutes
运行代码:gcc 4.4.6需要不到一秒的gcc 3.2.3需要超过6分钟
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;
}
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.