<tfoot id='wqcg2'></tfoot>

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

        <bdo id='wqcg2'></bdo><ul id='wqcg2'></ul>
      <legend id='wqcg2'><style id='wqcg2'><dir id='wqcg2'><q id='wqcg2'></q></dir></style></legend>
      1. <i id='wqcg2'><tr id='wqcg2'><dt id='wqcg2'><q id='wqcg2'><span id='wqcg2'><b id='wqcg2'><form id='wqcg2'><ins id='wqcg2'></ins><ul id='wqcg2'></ul><sub id='wqcg2'></sub></form><legend id='wqcg2'></legend><bdo id='wqcg2'><pre id='wqcg2'><center id='wqcg2'></center></pre></bdo></b><th id='wqcg2'></th></span></q></dt></tr></i><div id='wqcg2'><tfoot id='wqcg2'></tfoot><dl id='wqcg2'><fieldset id='wqcg2'></fieldset></dl></div>
      2. C++ 优化器重新排序对时钟()的调用是否合法?

        时间:2023-09-16
        • <legend id='3QDRq'><style id='3QDRq'><dir id='3QDRq'><q id='3QDRq'></q></dir></style></legend>

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

            • <small id='3QDRq'></small><noframes id='3QDRq'>

                  <tbody id='3QDRq'></tbody>
                <tfoot id='3QDRq'></tfoot>
                • 本文介绍了C++ 优化器重新排序对时钟()的调用是否合法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  C++ 编程语言第 4 版,第 225 页读到:只要结果与简单的执行顺序相同,编译器就可以重新排序代码以提高性能.一些编译器,例如处于发布模式的 Visual C++,将重新排序此代码:

                  The C++ Programming Language 4th edition, page 225 reads: A compiler may reorder code to improve performance as long as the result is identical to that of the simple order of execution. Some compilers, e.g. Visual C++ in release mode, will reorder this code:

                  #include <time.h>
                  ...
                  auto t0 = clock();
                  auto r  = veryLongComputation();
                  auto t1 = clock();
                  
                  std::cout << r << "  time: " << t1-t0 << endl;
                  

                  变成这种形式:

                  auto t0 = clock();
                  auto t1 = clock();
                  auto r  = veryLongComputation();
                  
                  std::cout << r << "  time: " << t1-t0 << endl;
                  

                  保证与原始代码不同的结果(零与大于零时间报告).有关详细示例,请参阅我的其他问题.这种行为是否符合 C++ 标准?

                  which guarantees different result than original code (zero vs. greater than zero time reported). See my other question for detailed example. Is this behavior compliant with the C++ standard?

                  推荐答案

                  编译器无法交换两个 clock 调用.t1 必须设置在 t0 之后.这两个调用都是可观察到的副作用.只要观察结果与抽象机器的可能观察结果一致,编译器就可以对这些可观察到的效果之间的任何内容进行重新排序,甚至可以对可观察到的副作用进行重新排序.

                  The compiler cannot exchange the two clock calls. t1 must be set after t0. Both calls are observable side effects. The compiler may reorder anything between those observable effects, and even over an observable side effect, as long as the observations are consistent with possible observations of an abstract machine.

                  由于 C++ 抽象机并没有正式限制为有限速度,因此它可以在零时间内执行 veryLongComputation().执行时间本身并未定义为可观察的效果.实际实现可能与此相符.

                  Since the C++ abstract machine is not formally restricted to finite speeds, it could execute veryLongComputation() in zero time. Execution time itself is not defined as an observable effect. Real implementations may match that.

                  请注意,这个答案在很大程度上取决于 C++ 标准对编译器施加限制.

                  Mind you, a lot of this answer depends on the C++ standard not imposing restrictions on compilers.

                  这篇关于C++ 优化器重新排序对时钟()的调用是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 const 非整数指数优化 pow()? 下一篇:禁用 g++ 的返回值优化

                  相关文章

                  最新文章

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

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