<small id='2LiIZ'></small><noframes id='2LiIZ'>

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

      1. 读取输入文件,最快的方法?

        时间:2023-10-08

            <tbody id='CEESn'></tbody>

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

            <bdo id='CEESn'></bdo><ul id='CEESn'></ul>
          • <tfoot id='CEESn'></tfoot>
              1. <legend id='CEESn'><style id='CEESn'><dir id='CEESn'><q id='CEESn'></q></dir></style></legend>
                <i id='CEESn'><tr id='CEESn'><dt id='CEESn'><q id='CEESn'><span id='CEESn'><b id='CEESn'><form id='CEESn'><ins id='CEESn'></ins><ul id='CEESn'></ul><sub id='CEESn'></sub></form><legend id='CEESn'></legend><bdo id='CEESn'><pre id='CEESn'><center id='CEESn'></center></pre></bdo></b><th id='CEESn'></th></span></q></dt></tr></i><div id='CEESn'><tfoot id='CEESn'></tfoot><dl id='CEESn'><fieldset id='CEESn'></fieldset></dl></div>
                  本文介绍了读取输入文件,最快的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有许多浮点数形式的数据文本文件.我正在寻找用 C++ 读取它们的最快方法.如果这是最快的,我可以将文件更改为二进制文件.

                  I have numerous text files of data in the form of float numbers. I'm looking for the fastest way to read them in C++. I can change the file to binary if that's the fastest.

                  如果您能给我提示或将我推荐给一个有完整解释的网站,那就太好了.我不知道是否有任何库可以快速完成工作.即使有任何开源软件可以完成这项工作,也会有所帮助.

                  It would be great if you could give me hint or refer me to a website with complete explanation. I don't know whether there is any library that does the work fast. Even if there is any open source software that does the work, that would be helpful.

                  推荐答案

                  拥有二进制文件是最快的选择.您不仅可以在单个操作中使用原始 istream::read 直接在数组中读取它(非常快),而且如果您的操作系统支持,您甚至可以将文件映射到内存中;您可以在 POSIX 系统上使用 open/mmapCreateFile/CreateFileMapping/MapViewOfFile在 Windows 上,甚至是 Boost 跨平台解决方案(感谢 @Cory Nelson 指出).

                  Having a binary file is the fastest option. Not only you can read it directly in an array with a raw istream::read in a single operation (which is very fast), but you can even map the file in memory if your OS supports it; you can use open/mmap on POSIX systems, CreateFile/CreateFileMapping/MapViewOfFile on Windows, or even the Boost cross-platform solution (thanks @Cory Nelson for pointing it out).

                  快速&脏示例,假设文件包含一些 float 的原始表示:

                  Quick & dirty examples, assuming the file contains the raw representation of some floats:

                  正常"阅读:

                  #include <fstream>
                  #include <vector>
                  
                  // ...
                  
                  // Open the stream
                  std::ifstream is("input.dat");
                  // Determine the file length
                  is.seekg(0, std::ios_base::end);
                  std::size_t size=is.tellg();
                  is.seekg(0, std::ios_base::beg);
                  // Create a vector to store the data
                  std::vector<float> v(size/sizeof(float));
                  // Load the data
                  is.read((char*) &v[0], size);
                  // Close the file
                  is.close();
                  

                  使用共享内存:

                  #include <boost/interprocess/file_mapping.hpp>
                  #include <boost/interprocess/mapped_region.hpp>
                  
                  using boost::interprocess;
                  
                  // ....
                  
                  // Create the file mapping
                  file_mapping fm("input.dat", read_only);
                  // Map the file in memory
                  mapped_region region(fm, read_only);
                  // Get the address where the file has been mapped
                  float * addr = (float *)region.get_address();
                  std::size_t elements  = region.get_size()/sizeof(float);
                  

                  这篇关于读取输入文件,最快的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 C++ 中读取格式化输入的最简单方法? 下一篇:没有了

                  相关文章

                  最新文章

                • <tfoot id='kFc1k'></tfoot>
                • <small id='kFc1k'></small><noframes id='kFc1k'>

                • <legend id='kFc1k'><style id='kFc1k'><dir id='kFc1k'><q id='kFc1k'></q></dir></style></legend>

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

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