Does C/C++ offer any guarantee on minimal execution time?

后端 未结 4 440
心在旅途
心在旅途 2021-02-02 07:04

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

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-02 07:29

    There is no minimal execution time for a C or C++ executable because execution time depends on many platform specific issues such as:

    1. Processor clock rate.
    2. Clock cycles per instruction.
    3. Internal processor execution optimizations.
    4. Interruptions.
    5. Processor instruction set / capabilities.

    Some processors support multiplication, others don't. The processors that don't support multiplication would take longer to execute a program than a process that has multiplication instructions. Same with floating point.

    The internal operating speed of a processor varies. There is a common unit of time measurement called a "clock cycle". Most processor vendors specify the duration of an instruction in clock cycles. This measurement may be difficult due to internal support, such as cache management.

    Some processors have logic that can optimize the execution of instructions, or instruction patterns. One optimization is branch prediction.

    Many platforms have interrupts. For example, there may be a "system tick" interrupt which allows the operating system to know when to switch execution to another program. Some are not so periodic, such as when I/O occurs. A minimum execution time cannot be guaranteed when the program gets interrupted.

    Stating a minimum execution time would play havoc with the C and C++ language portability. Some platforms would want to execute code faster than the minimum time. Other platforms may not be able to achieve a minimum execution time (but they could benefit from a high level language like C).

    Also, how would the time be measured?

    Does the minimum execution time apply for delay loops or polling?

提交回复
热议问题