<tfoot id='U9HY0'></tfoot>

      <small id='U9HY0'></small><noframes id='U9HY0'>

      <legend id='U9HY0'><style id='U9HY0'><dir id='U9HY0'><q id='U9HY0'></q></dir></style></legend>

      • <bdo id='U9HY0'></bdo><ul id='U9HY0'></ul>

    1. <i id='U9HY0'><tr id='U9HY0'><dt id='U9HY0'><q id='U9HY0'><span id='U9HY0'><b id='U9HY0'><form id='U9HY0'><ins id='U9HY0'></ins><ul id='U9HY0'></ul><sub id='U9HY0'></sub></form><legend id='U9HY0'></legend><bdo id='U9HY0'><pre id='U9HY0'><center id='U9HY0'></center></pre></bdo></b><th id='U9HY0'></th></span></q></dt></tr></i><div id='U9HY0'><tfoot id='U9HY0'></tfoot><dl id='U9HY0'><fieldset id='U9HY0'></fieldset></dl></div>

        从输入迭代器创建 C++ std::string 的性能

        时间:2024-08-13

          <tfoot id='dDIBJ'></tfoot>

            <bdo id='dDIBJ'></bdo><ul id='dDIBJ'></ul>

            <small id='dDIBJ'></small><noframes id='dDIBJ'>

              <tbody id='dDIBJ'></tbody>
            <legend id='dDIBJ'><style id='dDIBJ'><dir id='dDIBJ'><q id='dDIBJ'></q></dir></style></legend>
                  <i id='dDIBJ'><tr id='dDIBJ'><dt id='dDIBJ'><q id='dDIBJ'><span id='dDIBJ'><b id='dDIBJ'><form id='dDIBJ'><ins id='dDIBJ'></ins><ul id='dDIBJ'></ul><sub id='dDIBJ'></sub></form><legend id='dDIBJ'></legend><bdo id='dDIBJ'><pre id='dDIBJ'><center id='dDIBJ'></center></pre></bdo></b><th id='dDIBJ'></th></span></q></dt></tr></i><div id='dDIBJ'><tfoot id='dDIBJ'></tfoot><dl id='dDIBJ'><fieldset id='dDIBJ'></fieldset></dl></div>

                  本文介绍了从输入迭代器创建 C++ std::string 的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在做一些非常简单的事情:将整个文本文件从磁盘拖入 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() 计算文件大小和 reserve() 会不会更好(也就是更快)> 在读取之前字符串中有那么多空间?

                  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?

                  推荐答案

                  我对您的实现 (1)、我的 (2) 以及我在 stackoverflow 上找到的其他两个(3 和 4)进行了基准测试.

                  I benchmarked your implementation(1), mine(2), and two others(3 and 4) that I found on stackoverflow.

                  结果(100 次运行的平均值;使用 gettimeofday 计时,文件是 40 段 lorem ipsum):

                  Results (Average of 100 runs; timed using gettimeofday, file was 40 paragraphs of lorem ipsum):

                  • 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 的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Unix 上将 C++ std::clog 重定向到 syslog 下一篇:如何像 std C++ 流一样使用我的日志记录类?

                  相关文章

                • <i id='LFGmx'><tr id='LFGmx'><dt id='LFGmx'><q id='LFGmx'><span id='LFGmx'><b id='LFGmx'><form id='LFGmx'><ins id='LFGmx'></ins><ul id='LFGmx'></ul><sub id='LFGmx'></sub></form><legend id='LFGmx'></legend><bdo id='LFGmx'><pre id='LFGmx'><center id='LFGmx'></center></pre></bdo></b><th id='LFGmx'></th></span></q></dt></tr></i><div id='LFGmx'><tfoot id='LFGmx'></tfoot><dl id='LFGmx'><fieldset id='LFGmx'></fieldset></dl></div>
                • <tfoot id='LFGmx'></tfoot>
                    <bdo id='LFGmx'></bdo><ul id='LFGmx'></ul>
                  1. <legend id='LFGmx'><style id='LFGmx'><dir id='LFGmx'><q id='LFGmx'></q></dir></style></legend>

                    1. <small id='LFGmx'></small><noframes id='LFGmx'>