Why do compilers seems to be polite toward loops that do nothing and do not eliminate them?
Does the C standard require loops to take some time?
Example, the f
You did not specify the compiler, but let's assume it's gcc
.
gcc does not remove empty loops, at least not according to the documentation. It contains the following text:
Historically, GCC has not deleted “empty” loops under the assumption that the most likely reason you would put one in a program is to have a delay, so deleting them will not make real programs run any faster.
However, it can remove empty loops if they are "emptied" by the optimizer, that is, if the loop contains code that the optimizer can move outside the loop, and the resulting loop is empty.
It is not clear from the documentation if this is still true in the most recent version. The manual mentions "historically" without specifying why. If you update your question with information about your exact platform and compiler, maybe a better answer can be given.