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

  1. <tfoot id='cXI78'></tfoot>
      <bdo id='cXI78'></bdo><ul id='cXI78'></ul>
  2. <small id='cXI78'></small><noframes id='cXI78'>

  3. <legend id='cXI78'><style id='cXI78'><dir id='cXI78'><q id='cXI78'></q></dir></style></legend>

    1. C 和 C++ 样式文件 IO 之间的性能差异

      Performance Difference Between C and C++ Style File IO(C 和 C++ 样式文件 IO 之间的性能差异)

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

            2. <legend id='j02tu'><style id='j02tu'><dir id='j02tu'><q id='j02tu'></q></dir></style></legend>
                <tbody id='j02tu'></tbody>
            3. <small id='j02tu'></small><noframes id='j02tu'>

              • <tfoot id='j02tu'></tfoot>
                本文介绍了C 和 C++ 样式文件 IO 之间的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我一直听说 C++ 文件 I/O 操作比 C 风格 I/O 慢得多.但是我没有找到任何关于它们实际上有多慢的实用参考,所以我决定在我的机器上测试它(Ubuntu 12.04、GCC 4.6.3、ext4 分区格式).

                I've always heard that C++ file I/O operations are much much slower than C style I/O. But I didn't find any practical references on comparatively how slow they actually are, so I decided to test it in my machine (Ubuntu 12.04, GCC 4.6.3, ext4 partition format).

                首先我在磁盘中写入了一个 ~900MB 的文件.

                First I wrote a ~900MB file in the disk.

                C++ (ofstream):163s

                C++ (ofstream): 163s

                ofstream file("test.txt");
                    
                for(register int i = 0; i < 100000000; i++) 
                    file << i << endl;
                

                C (fprintf):12s

                FILE *fp = fopen("test.txt", "w");
                    
                for(register int i = 0; i < 100000000; i++) 
                    fprintf(fp, "%d
                ", i);
                

                我期待这样的输出,它表明在 C++ 中写入文件比在 C 中慢得多.然后我使用 C 和 C++ I/O 读取同一个文件.是什么让我惊叹从文件读取时性能几乎没有差异.

                I was expecting such output, it shows that writing to a file is much slower in C++ than in C. Then I read the same file using C and C++ I/O. What made me exclaimed that there is almost no difference in performance while reading from file.

                C++ (ifstream):12s

                int n;
                ifstream file("test.txt");
                
                for(register int i = 0; i < 100000000; i++) 
                    file >> n;
                

                C (fscanf):12s

                FILE *fp = fopen("test.txt", "r");
                    
                for(register int i = 0; i < 100000000; i++) 
                    fscanf(fp, "%d", &n);
                

                那么,为什么使用流执行写入要花这么长时间?或者,为什么使用流读取比写入快?

                So, why is taking so long to execute writing using stream? Or, why reading using stream is so fast compared to writing?

                结论:罪魁祸首是 std::endl,正如答案和评论所指出的那样.换线<代码>文件<<我<<endl;到<代码>文件<<我<<' '; 已将运行时间从 163 秒减少到 16 秒.

                Conclusion: The culprit is the std::endl, as the answers and the comments have pointed out. Changing the line file << i << endl; to file << i << ' '; has reduced running time to 16s from 163s.

                推荐答案

                您正在使用 endl 打印换行符.这就是问题所在,因为它更多不仅仅是打印换行符 —endl刷新缓冲区,这是一个昂贵的操作(如果你在每次迭代中都这样做).

                You're using endl to print a newline. That is the problem here, as it does more than just printing a newline — endl also flushes the buffer which is an expensive operation (if you do that in each iteration).

                如果您的意思是这样,请使用 :

                Use if you mean so:

                file << i << '
                ';
                

                此外,必须在发布模式下编译您的代码(即打开优化).

                And also, must compile your code in release mode (i.e turn on the optimizations).

                这篇关于C 和 C++ 样式文件 IO 之间的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                Delay Loading DLLs(延迟加载 DLL)
                Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                Loading a dll from a dll?(从 dll 加载 dll?)
                <tfoot id='BZn3B'></tfoot>
                <i id='BZn3B'><tr id='BZn3B'><dt id='BZn3B'><q id='BZn3B'><span id='BZn3B'><b id='BZn3B'><form id='BZn3B'><ins id='BZn3B'></ins><ul id='BZn3B'></ul><sub id='BZn3B'></sub></form><legend id='BZn3B'></legend><bdo id='BZn3B'><pre id='BZn3B'><center id='BZn3B'></center></pre></bdo></b><th id='BZn3B'></th></span></q></dt></tr></i><div id='BZn3B'><tfoot id='BZn3B'></tfoot><dl id='BZn3B'><fieldset id='BZn3B'></fieldset></dl></div>

                    <tbody id='BZn3B'></tbody>
                  • <legend id='BZn3B'><style id='BZn3B'><dir id='BZn3B'><q id='BZn3B'></q></dir></style></legend>

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

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