C++ 优化器重新排序对时钟()的调用是否合法?

2021-12-20 00:00:00 optimization c++ clock

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.

相关文章