国内“为”的固有效率低下环? [英] Inherent inefficiency in domestic "for" loop?

查看:79
本文介绍了国内“为”的固有效率低下环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




C的国内用法是用于

简单增量循环效率低下?这是一个非常简单的程序,它打印出一个字节中的

位数。


#include< stdio.h>

#include< limits.h>

#include< stdlib.h>


int main(无效)

{

未签名i;


for(i = 0; i!= CHAR_BIT; ++ i)

{

printf(" Bit Index%u \ n,i);

}


system("暂停);

}

(随意用i< CHAR_BIT替换i!= CHAR_BIT。)

如果我尝试使用goto复制它,我会得到以下结果:


#include< stdio.h>

#include< limits.h>

#include< stdlib.h>


int main(无效)

{

unsigned i;

Loop_Enter:


i = 0;


Loop_Condition:


if(!(i!= CHAR_BIT))goto Loop_End;


Loop_正文:


printf(比特指数%u \ n,i);


Loop_Continue:


++ i;

转到Loop_Condition;


Loop_End:


;

system(PAUSE);

}

但是,我们可以看到第一个条件测试是多余的 -

最好在每次迭代后测试条件,而不是之前的b $ b。类似于:


#include< stdio.h>

#include< limits.h>

#include< ; stdlib.h>


int main(无效)

{

未签名i;

Loop_Enter:


i = 0;


Loop_Body:


printf(" Bit Index% u $ \\ n,i);

Loop_Continue:


++ i;


Loop_Condition:


if(i!= CHAR_BIT)goto Loop_Body;


Loop_End:


;

system(PAUSE);

}

如果我们比较两个代码片段的执行情况,我们可以看到:

片段1:测试条件9次

片段2:测试条件8次

广泛使用的C方法简单的循环增量本身就是

效率低下?乍一看,对我来说似乎是这样。

(是的,我意识到大多数编译器会展开那个循环,所以让我们假设我们正在工作有一个更复杂的循环,其数量为
迭代,直到运行时才确定。)

-


Frederick Gotham



Is the domestic usage of the C "for" loop inefficient when it comes to
simple incrementation? Here''s a very simple program that prints out the
bit-numbers in a byte.

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

int main(void)
{
unsigned i;

for( i = 0; i != CHAR_BIT; ++i )
{
printf( "Bit Index %u\n", i );
}

system("PAUSE");
}
(Feel free to substitute "i != CHAR_BIT" with "i < CHAR_BIT".)
If I try to replicate that using "goto", I get the following:

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

int main(void)
{
unsigned i;
Loop_Enter:

i = 0;

Loop_Condition:

if ( !(i != CHAR_BIT) ) goto Loop_End;

Loop_Body:

printf( "Bit Index %u\n", i );

Loop_Continue:

++i;
goto Loop_Condition;

Loop_End:

;
system("PAUSE");
}
However, we can see that the very first conditional test is redundant --
it would be better to test the condition AFTER each iteration, rather
than before. Something like:

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

int main(void)
{
unsigned i;
Loop_Enter:

i = 0;

Loop_Body:

printf( "Bit Index %u\n", i );
Loop_Continue:

++i;

Loop_Condition:

if ( i != CHAR_BIT ) goto Loop_Body;

Loop_End:

;
system("PAUSE");
}
If we compare the execution of both code snippets, we can see:
Snippet 1: Tests the condition 9 times
Snippet 2: Tests the condition 8 times
Is the widely used C method of simple loop incrementation inherently
inefficient? At first glance, it appears so to me.
(Yes, I realise that most compilers will "unroll" that loop, so lets
pretend we''re working with a more complicated loop whose amount of
iterations isn''t determined until runtime.)
--

Frederick Gotham

推荐答案

Frederick Gotham< fg ******* @ SPAM.com>写道:
Frederick Gotham <fg*******@SPAM.com> writes:
C的国内用法是用于
简单增量循环效率低吗?这是一个非常简单的程序,它打印出一个字节的
位数。


[...]

然而,我们可以看到第一个条件测试是多余的 -
测试会更好每次迭代后的条件,而不是之前的条件。类似于:


[...]

广泛使用的C方法简单的循环增量本身是否效率低?乍一看,它对我来说似乎是这样。
Is the domestic usage of the C "for" loop inefficient when it comes to
simple incrementation? Here''s a very simple program that prints out the
bit-numbers in a byte.
[...]
However, we can see that the very first conditional test is redundant --
it would be better to test the condition AFTER each iteration, rather
than before. Something like:
[...]
Is the widely used C method of simple loop incrementation inherently
inefficient? At first glance, it appears so to me.




仅当循环总是至少执行一次时。在更多

一般情况下,你有类似

的东西(i = start; i< end; i ++)

然后你做想要在第一次迭代之前进行测试。


我永远不会将循环转换为糟糕的标签形式+

你建议的那些。如果你确实想在第一次迭代中避免测试,那么使用do {...} while循环。但是这个

通常是毫无意义的微优化。


另外,我不知道你为什么把它称为国内。对于

循环。在这种情况下,这个词没有意义。

-

IMO,Perl是一个很好的语言,可以让你挣扎

--Micah Cowan



Only if the loop always executes at least once. In the more
general case where you have something like
for (i = start; i < end; i++)
then you do want the test before the first iteration.

I would never translate a loop into the awful form of labels +
gotos that you suggest. Use a do { ... } while loop if you
really want to avoid the test on the first iteration. But this
is usually a pointless micro-optimization.

Also, I have no idea why you refer to this as the "domestic" for
loop. The word does not make sense in this context.
--
"IMO, Perl is an excellent language to break your teeth on"
--Micah Cowan


文章< Xn ************************* *@194.125.133.14> ;,

Frederick Gotham< fg ******* @ SPAM.com>写道:
In article <Xn**************************@194.125.133.14>,
Frederick Gotham <fg*******@SPAM.com> wrote:
然而,我们可以看到第一个条件测试是多余的 -
However, we can see that the very first conditional test is redundant --




在这种情况下编译器也可以看到它。


- Richard



In cases like this the compiler can see it too.

-- Richard


* Ben Pfaff - >有人:
* Ben Pfaff -> Someone:
另外,我不知道为什么你把它称为国内和国内。用于
循环。这个词在这种情况下没有意义。
Also, I have no idea why you refer to this as the "domestic" for
loop. The word does not make sense in this context.




似乎一些人工智能故事产生的程序是松散的。

他们'被垃圾邮件发送者和一些Usenet海报使用,我想加上一些

人类修复。一些诗歌在[no.test]中出现的是

太棒了。


-

答:因为它弄乱了订单人们通常会阅读文字。

问:为什么这么糟糕?

A:热门帖子。

问:什么usenet和电子邮件中最烦人的事情是什么?



It seems that some AI story-generating programs are on the loose.
They''re used by spammers and some Usenet posters, I guess with some
human fixup added. Some of the "poetry" that turns up in [no.test] is
amazing.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于国内“为”的固有效率低下环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆