for循环的效率 [英] Efficiency of the for loop

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

问题描述

大家好,

这是关于代码效率的问题。

a: -

int i;

for(i = 0; i< 20; i ++)

printf("%d",i);

b: -

int i = 10;

for(i = 0; i< 10; i ++){

printf("%d",i);

printf("%d",i);

}

这些代码中的哪一个会运行得更快?哪一个会产生小的b $ b b可执行文件大小?

如果我用一些算术表达式替换printf语句,

对前面的问题的答案是否会改变?


任何人都可以建议任何链接,我可以在这里了解更多关于写作

效果的C代码?


谢谢提前回复.....

问候,

Vamshi。

Hi all,
This is a question about the efficiency of the code.
a :-
int i;
for( i = 0; i < 20; i++ )
printf("%d",i);
b:-
int i = 10;
for( i = 0; i < 10; i++ ){
printf("%d",i);
printf("%d",i);
}
Which one of these code will run faster? which one will generate small
executable size?
if I replace printf statements with some arithmatic expression will the
answer for the previous questions changes ?

Can anyone suggest any links where i can learn more about writing
efficent C code ?

Thanks in advance for the replies.....
Regards,
Vamshi.

推荐答案

vamshi写道:
vamshi wrote:

大家好,

这是一个关于代码效率的问题。

a: -

int i;

for(i = 0; i< 20; i ++)

printf("% d",i);


b: -

int i = 10;

for(i = 0; i< 10; i ++){

printf("%d",i);

printf("%d",i);

}


这些代码中的哪一个会运行得更快?哪一个会产生小的b $ b b可执行文件大小?

如果我用一些算术表达式替换printf语句,

对前面的问题的答案是否会改变?
Hi all,
This is a question about the efficiency of the code.
a :-
int i;
for( i = 0; i < 20; i++ )
printf("%d",i);
b:-
int i = 10;
for( i = 0; i < 10; i++ ){
printf("%d",i);
printf("%d",i);
}
Which one of these code will run faster? which one will generate small
executable size?
if I replace printf statements with some arithmatic expression will the
answer for the previous questions changes ?



您的测量结果告诉您什么?

What did your measurements tell you?


任何人都可以建议任何我可以了解更多信息的链接写作

efficent C代码?
Can anyone suggest any links where i can learn more about writing
efficent C code ?



只有在必要时才写清洁代码并担心效率。


-

Ian Collins。

Write clean code and worry about efficiency only if you have to.

--
Ian Collins.


" vamshi" < te ********* @ gmail.comwrote:
"vamshi" <te*********@gmail.comwrote:

这是一个关于代码效率的问题。
This is a question about the efficiency of the code.



因此,它对于这个新闻组来说是偏离主题的,因为ISO C不会因为效率问题而关注
。这也是一个很好的理由

:这样的问题很少有答案在任何地方都是正确的。

And as such, it is off-topic for this newsgroup, because ISO C does not
concern itself with matters of efficiency. There''s a good reason for
this, too: such questions rarely have an answer that is true everywhere.


a: -

int i;

for(i = 0; i< 20; i ++)

printf("%d",i);


b: -

int i = 10;

for(i = 0; i< 10; i ++){

printf("%d",i);

printf("%d",i);

}


这些代码中的哪一个会运行得更快?哪一个会生成小的b $ b可执行文件大小?
a :-
int i;
for( i = 0; i < 20; i++ )
printf("%d",i);
b:-
int i = 10;
for( i = 0; i < 10; i++ ){
printf("%d",i);
printf("%d",i);
}

Which one of these code will run faster? which one will generate small
executable size?



没人知道。他们不会做同样的事情,一开始,这使得比较相当无意义。如果你重写了它们,那么任何

编译器都可以自由地将它们编译成相同的代码。大多数情况下,它很可能是b $ b,但即便如此,也不可能分辨出哪些代码

编译得更快和/或更小。事实上,答案很可能会改变你使用的编译器,而且还有你编译的平台,你编译的平台_for_,以及你使用的编译器选项

使用。

最后,这种微优化很少是你程序效率的最大因素。正确地调用你的编译器可能或者可能没有更多的影响,但是有助于你编写更有效代码的一件事就是简单地选择正确的算法。代码段A和代码段B之间的差异为2%

与选择相比相形见
另一种算法可以将代码段A或B调用20%。

如果你已经从你的

算法中挤出了所有优化,并且真的必须具有最后2%的效率或者最后2%

更小的代码大小,那么是你必须记住的两个规则:

- 测量,测量,测量和_then_选择最适合_your_特殊情况的功能

;

- 当您将代码移植到另一个实现时,期望必须撤消并重做所有微优化(这可能与

下一个错误修正版本一样微不足道您的编译器。)


Richard

Nobody knows. They don''t do the same thing, to begin with, which makes
the comparison rather pointless. If you rewrote them so they did, any
compiler would be free to compile them to the same code. It''s likely
that most do not, but even so, it is impossible to tell which code
compiles faster and/or smaller. In fact, the answer is likely to change
not only with the compiler you use, but also with the platform you
compile on, the platform you compile _for_, and the compiler options you
use.
In the end, such micro-optimisation is rarely the largest factor in the
efficiency of your program. Calling your compiler correctly may or may
not be of more influence, but the one thing which will help you write
more efficient code is to simply choose the correct algorithm. A 2%
difference between snippet A and snippet B is dwarfed by the choice for
another algorithm which calls snippet A or B 20% fewer.
If you have already squeezed all the optimisation out of your
algorithms, and really must have that last 2% efficiency or that last 2%
smaller code size, there are two rules that you must memorise:
- Measure, measure, measure, and _then_ choose the function which is
best optimised for _your_ particular situation;
- Expect to have to undo and redo all micro-optimisations when you port
your code to another implementation (which may be as trivial as the
next bugfix version of your compiler).

Richard




vamshi写道:

vamshi wrote:

大家好,

这是关于代码效率的问题。

a: -

int i;

for(i = 0 ;我< 20; i ++)

printf("%d",i);


b: -

int i = 10;

for(i = 0; i< 10; i ++){

printf("%d",i);

printf(" ;%d",i);

}
Hi all,
This is a question about the efficiency of the code.
a :-
int i;
for( i = 0; i < 20; i++ )
printf("%d",i);
b:-
int i = 10;
for( i = 0; i < 10; i++ ){
printf("%d",i);
printf("%d",i);
}



你在第一次printf中错过了''i ++''吗?

did you miss ''i++'' in first printf ?


>


这些代码中的哪一个会运行得更快?哪一个会产生小的b $ b b可执行文件大小?

如果我用一些算术表达式替换printf语句,

对前面的问题的答案是否会改变?


任何人都可以建议任何链接,我可以在这里了解更多关于写作

效果的C代码?


谢谢提前回复.....

问候,

Vamshi。
>

Which one of these code will run faster? which one will generate small
executable size?
if I replace printf statements with some arithmatic expression will the
answer for the previous questions changes ?

Can anyone suggest any links where i can learn more about writing
efficent C code ?

Thanks in advance for the replies.....
Regards,
Vamshi.


这篇关于for循环的效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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