CPU 是否在无限循环期间空闲?
Is CPU idle during an infinite loop?
下面是书中的一段Java性能:权威指南。
You write a simple script containing an infinite loop. When that
script is executed, it will consume 100% of a CPU. The following batch
job will do just that in Windows:
ECHO OFF
:BEGIN
ECHO LOOPING
GOTO BEGIN
REM We never get here...
ECHO DONE
Consider what it would mean if this script did not consume 100% of a
CPU. It would mean that the operating system had something it could
do—it could print yet another line saying LOOPING, but it chose
instead to be idle. Being idle doesn’t help anyone in that case,
and if we were doing a useful (lengthy) calculation, forcing the CPU
to be periodically idle would mean that it would take longer to get
the answer we are after.
我很难理解为什么无限循环会使 CPU 保持空闲?
我认为您误解了文字 -- 无限循环会占用所有可用的 CPU 循环。
当书上说“但它选择空闲”时,它只是说如果无限循环脚本没有占用 100% 的 CPU ,那么它可能已经决定在某些周期内变得空闲(这种情况从来没有发生过,因为这是对可用处理能力的浪费)。
下面是书中的一段Java性能:权威指南。
You write a simple script containing an infinite loop. When that script is executed, it will consume 100% of a CPU. The following batch job will do just that in Windows:
ECHO OFF :BEGIN ECHO LOOPING GOTO BEGIN REM We never get here... ECHO DONE
Consider what it would mean if this script did not consume 100% of a CPU. It would mean that the operating system had something it could do—it could print yet another line saying LOOPING, but it chose instead to be idle. Being idle doesn’t help anyone in that case, and if we were doing a useful (lengthy) calculation, forcing the CPU to be periodically idle would mean that it would take longer to get the answer we are after.
我很难理解为什么无限循环会使 CPU 保持空闲?
我认为您误解了文字 -- 无限循环会占用所有可用的 CPU 循环。
当书上说“但它选择空闲”时,它只是说如果无限循环脚本没有占用 100% 的 CPU ,那么它可能已经决定在某些周期内变得空闲(这种情况从来没有发生过,因为这是对可用处理能力的浪费)。