for循环中的break语句 [英] break statement in a for loop

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

问题描述

我正在编写代码速度,而不是ansi或其他好东西

我遇到了以下问题:


当我有像这样的for循环:


b = b0;

for(a = 0,i = 0; i< 100; i ++,b--){

如果(b%i)继续;

a = 1;

}


我想要在== 1之后突破循环-fast-。当我休息时

之后就像


b = b0;

for(a = 0,i = 0; i< 100; i ++,b--){

如果(b%i)继续;

a = 1;

break;

}


以前的快速循环变成了可怕的(因子100左右......)慢了

一个,所以更好在我看来,根本不使用任何休息。


有没有人知道 - 为什么break语句使循环如此

慢?

这不仅仅是一个额外的jmp在汇编程序等效代码中?我比较了

两个组合输出有和没有break语句,但是他们

真的看起来很不一样......?!我使用gcc优化-O3。

I am programming for code-speed, not for ansi or other nice-guy stuff
and I encountered the following problem:

When I have a for loop like this:

b=b0;
for (a=0,i=0;i<100;i++,b--) {
if (b%i) continue;
a=1;
}

I want to break out of the loop -fast- after a==1. When I put a break
after it like in

b=b0;
for (a=0,i=0;i<100;i++,b--) {
if (b%i) continue;
a=1;
break;
}

the former fast loop turns into a horribly (factor 100 or so...) slow
one, so better to use no break at all in my opinion.

Does anyone know exactly -why- the break statement makes the loop so
slow??
Isn''t it just an extra "jmp" in assembler equivalent code? I compared
both assembled outputs with and without the break statement, but they
really look quite different.. ?! I optimize with -O3 using gcc.

推荐答案

28 huhti,22:15,a ... @ chello.nl写道:
On 28 huhti, 22:15, a...@chello.nl wrote:

我正在编写代码速度,而不是ansi或其他好东西

我遇到了以下问题:


当我有这样的for循环:


b = b0;

for(a = 0,i = 0; i <100; i ++,b--){

如果(b%i)继续;

a = 1;


}


我想在== 1之后突破循环-fast-。当我休息时

之后就像


b = b0;

for(a = 0,i = 0; i< 100; i ++,b--){

如果(b%i)继续;

a = 1;

break;


}


前快速循环变成可怕的(因子100左右......)慢

一在我看来,最好不要使用任何休息。


有没有人知道 - 为什么break语句使循环如此

慢?

这不仅仅是一个额外的jmp在汇编程序等效代码中?我比较了

两个组合输出有和没有break语句,但是他们

真的看起来很不一样......?!我使用gcc优化-O3。
I am programming for code-speed, not for ansi or other nice-guy stuff
and I encountered the following problem:

When I have a for loop like this:

b=b0;
for (a=0,i=0;i<100;i++,b--) {
if (b%i) continue;
a=1;

}

I want to break out of the loop -fast- after a==1. When I put a break
after it like in

b=b0;
for (a=0,i=0;i<100;i++,b--) {
if (b%i) continue;
a=1;
break;

}

the former fast loop turns into a horribly (factor 100 or so...) slow
one, so better to use no break at all in my opinion.

Does anyone know exactly -why- the break statement makes the loop so
slow??
Isn''t it just an extra "jmp" in assembler equivalent code? I compared
both assembled outputs with and without the break statement, but they
really look quite different.. ?! I optimize with -O3 using gcc.



Break语句本身不应该使分支慢得多,

而在实践中通常会这样做。


我猜它是依赖于实现的,例如在x86中它不是一个很大的优惠。

只需简单明了的代码,它会在

机器代码级别中变得非常好。如果编译器足够智能,它应该抓住这种情况并为它制作适当的代码。不用担心。

Break statement should not inherently make branching that much slower,
while in practice it usually does.

I guess it''s implementation dependent, for example in x86 it''s not a
big deal.
Just make concise and clear code, and it will turn out exactly nice in
machine code level. If the compiler is intelligent enough, it should
catch this situation and make appropriate code for it. No worries.


a.***@chello.nl 说:

我正在为代码速度编程,而不是针对ansi或其他好东西的东西

我遇到了以下问题:


当我有这样的for循环:


b = b0;

for(a = 0,i = 0; i <100; i ++,b--){

如果(b%i)继续;

a = 1;

}
I am programming for code-speed, not for ansi or other nice-guy stuff
and I encountered the following problem:

When I have a for loop like this:

b=b0;
for (a=0,i=0;i<100;i++,b--) {
if (b%i) continue;
a=1;
}



这应该非常快,因为在第一次循环时你在
上得到零除错误。


制作正确的程序比制作快速程序更容易

正确。然后,通过寻求正确性开始。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

This should be really fast, since you get a divide by zero error on the
very first time through the loop.

It is easier to make a correct program fast than to make a fast program
correct. Start off, then, by seeking correctness.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.




< a。 ***@chello.nlwrote in message

news:11 ********************** @ e65g2000hsc.googlegr psps.com。 ..

<a.***@chello.nlwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...

>我正在为代码速度编程,而不是针对ansi或其他好东西的东西

我遇到了以下内容问题:


当我有这样的for循环:


b = b0;

for(a = 0,i = 0; i <100; i ++,b--){

如果(b%i)继续;

a = 1;

}


我想在== 1之后突破循环-fast-。当我休息时

之后就像


b = b0;

for(a = 0,i = 0; i< 100; i ++,b--){

如果(b%i)继续;

a = 1;

break;

}


以前的快速循环变成了可怕的(因子100左右......)慢了

一个,所以更好在我看来,根本不使用任何休息。


有没有人知道 - 为什么break语句使循环如此

慢?

这不仅仅是一个额外的jmp在汇编程序等效代码中?我比较了

两个组合输出有和没有break语句,但是他们

真的看起来很不一样......?!我使用gcc优化-O3。
>I am programming for code-speed, not for ansi or other nice-guy stuff
and I encountered the following problem:

When I have a for loop like this:

b=b0;
for (a=0,i=0;i<100;i++,b--) {
if (b%i) continue;
a=1;
}

I want to break out of the loop -fast- after a==1. When I put a break
after it like in

b=b0;
for (a=0,i=0;i<100;i++,b--) {
if (b%i) continue;
a=1;
break;
}

the former fast loop turns into a horribly (factor 100 or so...) slow
one, so better to use no break at all in my opinion.

Does anyone know exactly -why- the break statement makes the loop so
slow??
Isn''t it just an extra "jmp" in assembler equivalent code? I compared
both assembled outputs with and without the break statement, but they
really look quite different.. ?! I optimize with -O3 using gcc.



我认为其他地方肯定会有一些错误。

如果循环执行因某个因素而改变当然,要么你不是在测量同样的东西,或者编译器已经优化了循环,一个案例而不是另一个案例。例如,如果您在函数中稍后使用了i

的值但忽略了a。在一种情况下,编译器可以优化

到i = 100,在另一种情况下,它可能不够聪明,而不是通过循环来执行
。 br />
有人可能会正确地指出,符合标准的编译器可以将
编译成一个非常慢的东西。该标准没有

保证效率。虽然这可能占两个因素或者b
三,我看不出它会如何占到100.

-

免费游戏和编程好东西。
http://www.personal.leeds .ac.uk / ~bgy1mm

I think there must be some mistake elsewhere.
If the loop execution has changed by a factor of 100 then either you are not
measuring the same thing, or the compiler has optimised the loop away in
one case but not the other. An example would be if you used the value of i
later in the function but ignored a. In one case the compiler can optimise
to i = 100, in the other case it probably isn''t clever enough to do it
without running through the loop.
Someone will probably point out, correctly, that a conforming compiler can
compile the break to something horribly slow. The standard makes no
guarantees on efficiency. Whilst this might account for a factor of two or
three, I cannot see how it would account for 100.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


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

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