测试环境
- Ubuntu 16.04
- clang 3.8.0
- testfile.txt 142w行
1. C式+循环push_back写法
FILE *in = fopen("testfile.txt", "r");
char ch;
auto str = new string;
while ((ch = fgetc(in) != EOF)) {
str->push_back(ch);
}
fclose(in);
运行时间:0.261s
2. istreambuf_iterator
ifstream in("testfile.txt", ios::in);
istreambuf_iterator<char> beg(in), end;
string str(beg, end);
in.close();
运行时间:0.945s
3. ostringstream
ifstream in("testfile.txt", ios::in);
ostringstream buf;
buf << in.rdbuf();
auto str = new string(buf.str());
in.close();
运行时间:0.036s