为什么编译器似乎对什么都不做也不消除它们的循环有礼貌?
C 标准是否要求循环需要一些时间?
例如,如下代码:
void foo(void) {
while(1) {
for(int k = 0; k < 1000000000; ++k);
printf("Foo\n");
}
}
运行速度比这个慢:
void foo(void) {
while(1) {
for(int k = 0; k < 1000; ++k);
printf("Foo\n");
}
}
即使是 -O3
优化级别。
我希望删除允许的空循环,从而在两个代码上获得相同的速度。
“所花费的时间”是应该由编译器保留的副作用吗?
最佳答案
不,所花费的时间不算作受 as-if 规则保护的可观察行为:
[C++14: 1.8/5]:
A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input. However, if any such execution contains an undefined operation, this International Standard places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation).
[C++14: 1.5/8]:
The least requirements on a conforming implementation are:
- Access to volatile objects are evaluated strictly according to the rules of the abstract machine.
- At program termination, all data written into files shall be identical to one of the possible results that execution of the program according to the abstract semantics would have produced.
- The input and output dynamics of interactive devices shall take place in such a fashion that prompting output is actually delivered before a program waits for input. What constitutes an interactive device is implementation-defined.
These collectively are referred to as the observable behavior of the program. [ Note: More stringent correspondences between abstract and actual semantics may be defined by each implementation. —end note ]
这些循环可以合法地优化出来,事实上,在某些情况下,标准故意试图让这样做更容易:
[C++14: 1.10/24]:
The implementation may assume that any thread will eventually do one of the following:
- terminate,
- make a call to a library I/O function,
- access or modify a volatile object, or
- perform a synchronization operation or an atomic operation.
[ Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. —end note ]
您的编译器实际上可能“礼貌”地注意到这些程序中循环的意图似乎是减慢重复文本输出的排放。 :)
关于c++ - C/C++ 是否提供最短执行时间的任何保证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36814199/