从输入迭代器创建C ++ std :: string的性能 [英] Performance of creating a C++ std::string from an input iterator
问题描述
我做的事情很简单:将整个文本文件从磁盘扔到 std :: string
。我当前的代码基本上这样做:
I'm doing something really simple: slurping an entire text file from disk into a std::string
. My current code basically does this:
std::ifstream f(filename);
return std::string(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
这不太可能会对程序产生任何性能影响,好奇这是否是一种缓慢的做法。
It's very unlikely that this will ever have any kind of performance impact on the program, but I still got curious whether this is a slow way of doing it.
有没有风险,字符串的构造将涉及很多重新分配?使用 seekg()
/ tellg()
来计算大小更好(即更快)
Is there a risk that the construction of the string will involve a lot of reallocations? Would it be better (that is, faster) to use seekg()
/tellg()
to calculate the size of the file and reserve()
that much space in the string before doing the reading?
推荐答案
我在stackoverflow上找到了你的实现(1),mine(2)和其他两个(3和4)。
I benchmarked your implementation(1), mine(2), and two others(3 and 4) that I found on stackoverflow.
100次运行;计时使用gettimeofday,文件是lorem ipsum的40段):
Results (Average of 100 runs; timed using gettimeofday, file was 40 paragraphs of lorem ipsum):
- readFile1:764
- readFile2:104
- readFile3:129
- readFile4:402
- readFile1: 764
- readFile2: 104
- readFile3: 129
- readFile4: 402
实现:
string readFile1(const string &fileName)
{
ifstream f(fileName.c_str());
return string(std::istreambuf_iterator<char>(f),
std::istreambuf_iterator<char>());
}
string readFile2(const string &fileName)
{
ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate);
ifstream::pos_type fileSize = ifs.tellg();
ifs.seekg(0, ios::beg);
vector<char> bytes(fileSize);
ifs.read(&bytes[0], fileSize);
return string(&bytes[0], fileSize);
}
string readFile3(const string &fileName)
{
string data;
ifstream in(fileName.c_str());
getline(in, data, string::traits_type::to_char_type(
string::traits_type::eof()));
return data;
}
string readFile4(const std::string& filename)
{
ifstream file(filename.c_str(), ios::in | ios::binary | ios::ate);
string data;
data.reserve(file.tellg());
file.seekg(0, ios::beg);
data.append(istreambuf_iterator<char>(file.rdbuf()),
istreambuf_iterator<char>());
return data;
}
这篇关于从输入迭代器创建C ++ std :: string的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!