循环选择 [英] Looping Choice
问题描述
http:// fms。 komkon.org/EMUL8/HOWTO.html
我读过:
"分配初始值后,我们开始主循环:
for(;;)
{
注意这个循环也可以实现作为
while(CPUIsRunning)
{
其中CPUIsRunning是一个布尔变量。这有一定的优势,
因为你可以通过设置CPUIsRunning = 0来随时终止循环。
不幸的是,每次传递检查这个变量都需要很多
CPU时间,如果可能应该避免。另外,不要实现这个
循环作为
而(1)
{
因为在这种情况下,一些编译器会生成代码检查
是否为1。你当然不希望编译器在循环的每次传递中做这个不必要的工作。
我想阅读你的意见在这些陈述上:)
In
http://fms.komkon.org/EMUL8/HOWTO.html
I read:
"After initial values are assigned, we start the main loop:
for(;;)
{
Note that this loop can also be implemented as
while(CPUIsRunning)
{
where CPUIsRunning is a boolean variable. This has certain advantages,
as you can terminate the loop at any moment by setting CPUIsRunning=0.
Unfortunately, checking this variable on every pass takes quite a lot of
CPU time, and should be avoided if possible. Also, do not implement this
loop as
while(1)
{
because in this case, some compilers will generate code checking
whether 1 is true or not. You certainly don''t want the compiler to do
this unnecessary work on every pass of a loop."
I would like to read your opinions on these statements :)
推荐答案
Lorenzo Villari说:
Lorenzo Villari said:
在
http://fms.komkon.org /EMUL8/HOWTO.html
我读过:
"分配初始值后,我们启动主循环:
for(;;)
{
请注意,此循环也可以实现为
while(CPUIsRunning)
{
其中CPUIsRunning是一个布尔变量。这有一定的优势,
因为您可以通过设置CPUIsRunning = 0随时终止循环。
In
http://fms.komkon.org/EMUL8/HOWTO.html
I read:
"After initial values are assigned, we start the main loop:
for(;;)
{
Note that this loop can also be implemented as
while(CPUIsRunning)
{
where CPUIsRunning is a boolean variable. This has certain advantages,
as you can terminate the loop at any moment by setting CPUIsRunning=0.
是的。但它仍然是一个糟糕的名字。 LoopIsRunning会更好。
Yes. But it''s still a lousy name. LoopIsRunning would be better.
不幸的是,在每次传递中检查这个变量需要花费很多时间,并且应该避免可能。
Unfortunately, checking this variable on every pass takes quite a lot of
CPU time, and should be avoided if possible.
废话。如果循环中发生了很多事情,那么检查一个int是
epsilon努力。当你决定如何踢足球时,你是否考虑了你的队友施加的引力对路径扭曲的影响?b $ b?如果在循环中没有发生很多事情,那么
你可以节省一两个时间来测试一个int,对吗?
Nonsense. If there''s a lot happening in the loop, checking one int is
epsilon effort. When you''re deciding how to kick a football, do you take
into account the path-distorting effect of the gravitational force exerted
by your team-mates? And if there''s *not* a lot happening in the loop, then
you can spare a clock or two to test an int, right?
另外,不要实现这个
循环为
而(1)
{
因为在这种情况下,一些编译器会生成代码检查
是否为1。
Also, do not implement this
loop as
while(1)
{
because in this case, some compilers will generate code checking
whether 1 is true or not.
不,这不是原因。不使用它的原因是它可以产生一个完全毫无意义且令人讨厌的警告,而(;;)的等效构造避免了。如果你真的想要一个无限循环,那么(;;)就好了你想要的东西 - 如果你想让循环完成一段时间,那么你应该这样说b $ b通过插入一个条件表达式在你的循环控件中。
No, that''s not the reason. The reason not to use it is that it can generate
a completely pointless and annoying warning, which the equivalent
construct for(;;) avoids. If you really want an endless loop, for(;;) is
exactly what you want - and if you want the loop to finish some time, you
should say so in your loop control by inserting a conditional expression.
你当然不希望编译器做什么
每一个不必要的工作传递一个循环。
我想阅读你对这些陈述的看法:)
You certainly don''t want the compiler to do
this unnecessary work on every pass of a loop."
I would like to read your opinions on these statements :)
我的意见是这些陈述毫无价值。
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
My opinion is that those statements are of no value.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Lorenzo Villari写道:
Lorenzo Villari wrote:
< a rel =nofollowhref =http://fms.komkon.org/EMUL8/HOWTO.html\"target =_ blank> http://fms.komkon.org/EMUL8/HOWTO.html
我读过:
"分配初始值后,我们开始主循环:
for(;;)
{
请注意,这个循环也可以实现为
while(CPUIsRunning)
{
其中CPUIsRunning是一个布尔变量。这有一定的优势,
因为你可以通过设置CPUIsRunning = 0来随时终止循环。
不幸的是,每次传递检查这个变量都需要很多
CPU时间,如果可能应该避免。另外,不要实现这个
循环作为
而(1)
{
因为在这种情况下,一些编译器会生成代码检查
是否为1。你当然不希望编译器在循环的每次传递中做这个不必要的工作。
我想阅读你的意见在这些陈述:):
In
http://fms.komkon.org/EMUL8/HOWTO.html
I read:
"After initial values are assigned, we start the main loop:
for(;;)
{
Note that this loop can also be implemented as
while(CPUIsRunning)
{
where CPUIsRunning is a boolean variable. This has certain advantages,
as you can terminate the loop at any moment by setting CPUIsRunning=0.
Unfortunately, checking this variable on every pass takes quite a lot of
CPU time, and should be avoided if possible. Also, do not implement this
loop as
while(1)
{
because in this case, some compilers will generate code checking
whether 1 is true or not. You certainly don''t want the compiler to do
this unnecessary work on every pass of a loop."
I would like to read your opinions on these statements :)
这是胡说八道。检查CPUIsRunning的开销应该可以忽略不计。检查1的开销应该与任何
不错的编译器不存在。
我不是永远循环的粉丝。我想知道的第一件事是关于循环的
是如何终止的。 永久循环给出了
的印象,通常是错误的,它永远不会终止。出于这个原因,
我相信你应该总是至少移动一个条件
实际上导致循环终止进入循环条件本身;
最好是最常见的这种情况之一。它可能会使你的
代码变得非常复杂,但根据我的经验,通常情况并非如此。
我不喜欢为此目的使用布尔变量,除非每次设置
变量'的值至少读取2-3次。
It''s nonsense. The overhead of checking CPUIsRunning should be quite
negligible. The overhead of checking 1 should be non-existent with any
decent compiler.
I''m not a fan of "forever loops". One of the first things I want to know
about a loop is how it terminates. A "forever loop" gives the
impression, usually falsely, that it never terminates. For that reason,
I believe that you should always move at least one of the conditions
that actually causes loop termination into the loop condition itself;
preferably one of the most common such conditions. It might make your
code prohibitively complicated to do so, but in my experience that''s
usually not the case.
I don''t like using boolean variables for this purpose unless the
variable''s value is read at least 2-3 time for each time that it is set.
James Kuyper说:
< snip>
James Kuyper said:
<snip>
我是不是永远循环的粉丝。我想知道的第一件事是关于循环的
是如何终止的。 永久循环给出了
的印象,通常是错误的,它永远不会终止。
I''m not a fan of "forever loops". One of the first things I want to know
about a loop is how it terminates. A "forever loop" gives the
impression, usually falsely, that it never terminates.
如果找到有人谈论改变循环的感觉,那是多么令人耳目一新!
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
How refreshing to find someone talking sense about loops for a change!
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
这篇关于循环选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!