473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

break statement in a for loop

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.

Apr 28 '07 #1
26 3192
On 28 huhti, 22:15, a...@chello.nl wrote:
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 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.
Apr 28 '07 #2
a.***@chello.nl said:
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;
}
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.
Apr 28 '07 #3

<a.***@chello.nlwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
>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 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

Apr 28 '07 #4
a...@chello.nl wrote:
I am programming for code-speed, not for ansi or other nice-guy stuff
I don't know _what_ you are programming for, but you flunked your
maths test _and_ your programming test, didn't you? (And that's me
being a nice guy, you shouldn't meet me in non-ansi mode).
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;
}
As others mentioned, undefined behavior (most likely a crash)
immediately when you calculate b % 0 in the first iteration. If you
started the loop with i = 1, then a would be set to 1 immediately,
making the whole loop rather pointless.

Now assuming that you intended to do something sensible, like starting
the loop with i = 2: Note that (b % i) != 0 is exactly the same as ((b
+ i) % i) != 0. Inside the loop, i is increased and b decreased in
each iteration, whereas b + i remains constant. So it seems that you
are just checking whether a certain number has a factor less than 100.
You can do that significantly faster by checking divisibility by prime
numbers up to 100; in practice a hardcoded

a = (b % 2 && b % 3 && b % 5 && b % 7 && b % 11 ... && b % 97) ? 0 :
1;

would be faster by a factor ten again on many implementations.

Apr 28 '07 #5
Ok, this was a bad fast example, I admit.
Therefore below another example that shows the point.
int main()
{
unsigned long i,j,k,l,n;

for (l=0;l<10000000;l++) {
j=0;
k=10;
n=231;

for (i=1;n>3;j+=8,k+=j,n-=2) {
if (k%n) continue;
i=0;
break;
}
}
return 0;
}
Run this without the break statement and with the break statement and
look at execution time. On my 1.5GHz P4 it takes 1.3s and 13.9s
respectively (!). My store on the factor 100 incorporates a lot more
code.

Apr 29 '07 #6
a.***@chello.nl said:
Ok, this was a bad fast example, I admit.
Therefore below another example that shows the point.
int main()
{
unsigned long i,j,k,l,n;

for (l=0;l<10000000;l++) {
j=0;
k=10;
n=231;

for (i=1;n>3;j+=8,k+=j,n-=2) {
if (k%n) continue;
i=0;
break;
}
}
return 0;
}
Run this without the break statement and with the break statement and
look at execution time. On my 1.5GHz P4 it takes 1.3s and 13.9s
respectively (!). My store on the factor 100 incorporates a lot more
code.
The same result can be achieved much more quickly:

int main(void)
{
return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 29 '07 #7
It's not my objective to show you the full code; it's about the issue
of execution speed and "break".
Richard Heathfield schreef:
a.***@chello.nl said:
Ok, this was a bad fast example, I admit.
Therefore below another example that shows the point.
int main()
{
unsigned long i,j,k,l,n;

for (l=0;l<10000000;l++) {
j=0;
k=10;
n=231;

for (i=1;n>3;j+=8,k+=j,n-=2) {
if (k%n) continue;
i=0;
break;
}
}
return 0;
}
Run this without the break statement and with the break statement and
look at execution time. On my 1.5GHz P4 it takes 1.3s and 13.9s
respectively (!). My store on the factor 100 incorporates a lot more
code.

The same result can be achieved much more quickly:

int main(void)
{
return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 29 '07 #8
a.***@chello.nl said:
It's not my objective to show you the full code; it's about the issue
of execution speed and "break".
But break isn't the problem. The problem is a poorly-constructed
algorithm, which involves you performing many unnecessary calculations.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 29 '07 #9
a.***@chello.nl wrote:
It's not my objective to show you the full code; it's about the issue
of execution speed and "break".

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Apr 29 '07 #10
a.***@chello.nl wrote:
Ok, this was a bad fast example, I admit.
Therefore below another example that shows the point.
int main()
{
unsigned long i,j,k,l,n;

for (l=0;l<10000000;l++) {
j=0;
k=10;
n=231;

for (i=1;n>3;j+=8,k+=j,n-=2) {
if (k%n) continue;
i=0;
break;
}
}
return 0;
}
Run this without the break statement and with the break statement and
look at execution time. On my 1.5GHz P4 it takes 1.3s and 13.9s
respectively (!). My store on the factor 100 incorporates a lot more
code.
On my system, without the "break", it takes 0.001 seconds (according
to "time"). That's because without the break, the compiler realises
your program does nothing and your for loop is useless, so it removes
it altogether. How about an actual demonstration (a program that does
something useful, such as printing the result of your calculation,
whatever it is you're trying to calculate)?

Apr 29 '07 #11

<a.***@chello.nlwrote in message
news:11**********************@c35g2000hsg.googlegr oups.com...
It's not my objective to show you the full code; it's about the issue
of execution speed and "break".

It is perfectly reasonable to post a compileable snippet showing the
problem, but here we don't have enough to work on. It is hard to optimise a
function that doesn't actully do anything.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Apr 29 '07 #12
Malcolm McLean wrote:
<a.***@chello.nlwrote in message
>It's not my objective to show you the full code; it's about the
issue of execution speed and "break".
It is perfectly reasonable to post a compileable snippet showing
the problem, but here we don't have enough to work on. It is hard
to optimise a function that doesn't actully do anything.
Besides which, the loop does different things with the break
inserted.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Apr 29 '07 #13
In article <11**********************@l77g2000hsb.googlegroups .com>,
<a.***@chello.nlwrote:
>Run this without the break statement and with the break statement and
look at execution time. On my 1.5GHz P4 it takes 1.3s and 13.9s
respectively (!).
I get a similar result with GCC on a PPC Mac. It appears to just be
an optimisation effect: without the break the compiler can determine
enough about the inner for loop to optimise it away completely; with
the break it can't.

It's easy to construct artificial examples of this kind, but they are
rare in real programs, because if the compiler can optimise away a
loop, then the programmer probably won't write it in the first place.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 29 '07 #14
On Apr 29, 8:34 am, a...@chello.nl wrote:
It's not my objective to show you the full code; it's about the issue
of execution speed and "break".
Your first example was pure nonsense invoking undefined behavior at
the first step.

Your second example was a completely useless piece of code, where huge
portions of the code could be optimised away. All that you have been
testing is the capability of the compiler to detect nonsense code and
remove it. It seems your break statement affected the compilers
capability of detecting nonsense code. This might be of importance to
you, it is of no importance to those 99.9999 percent of programmers
who actually write more sensible code in the first place.

Apr 29 '07 #15

"christian.bau" <ch***********@cbau.wanadoo.co.ukwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
On Apr 29, 8:34 am, a...@chello.nl wrote:
>It's not my objective to show you the full code; it's about the issue
of execution speed and "break".

Your first example was pure nonsense invoking undefined behavior at
the first step.

Your second example was a completely useless piece of code, where huge
portions of the code could be optimised away. All that you have been
testing is the capability of the compiler to detect nonsense code and
remove it. It seems your break statement affected the compilers
capability of detecting nonsense code. This might be of importance to
you, it is of no importance to those 99.9999 percent of programmers
who actually write more sensible code in the first place.
That's not quite true

I would much rather write

y = x/factorial(6);

with the factorial function doing what you would expect than

y = x/FACT6;

or y = x/720;

if I could trust the compiler to optimise away the loop.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Apr 29 '07 #16
"Malcolm McLean" <re*******@btinternet.comwrites:
"christian.bau" <ch***********@cbau.wanadoo.co.ukwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
>On Apr 29, 8:34 am, a...@chello.nl wrote:
>>It's not my objective to show you the full code; it's about the issue
of execution speed and "break".

Your first example was pure nonsense invoking undefined behavior at
the first step.

Your second example was a completely useless piece of code, where huge
portions of the code could be optimised away. All that you have been
testing is the capability of the compiler to detect nonsense code and
remove it. It seems your break statement affected the compilers
capability of detecting nonsense code. This might be of importance to
you, it is of no importance to those 99.9999 percent of programmers
who actually write more sensible code in the first place.
That's not quite true

I would much rather write

y = x/factorial(6);

with the factorial function doing what you would expect than

y = x/FACT6;

or y = x/720;

if I could trust the compiler to optimise away the loop.
That's very different. In the case of the factorial() function, the
compiler can *optimize* the loop (possibly to a single literal value);
it can't *optimize away* the loop, because you use the result.

In the original code being discussed, the loop does not produce any
results. An optimizing compiler can eliminate it entirely.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 29 '07 #17

christian.bau schreef:
On Apr 29, 8:34 am, a...@chello.nl wrote:
It's not my objective to show you the full code; it's about the issue
of execution speed and "break".

Your first example was pure nonsense invoking undefined behavior at
the first step.

Your second example was a completely useless piece of code, where huge
portions of the code could be optimised away. All that you have been
testing is the capability of the compiler to detect nonsense code and
remove it. It seems your break statement affected the compilers
capability of detecting nonsense code. This might be of importance to
you, it is of no importance to those 99.9999 percent of programmers
who actually write more sensible code in the first place.
Boy, oh boy... I thought there would be some intelligent people here
who would
know what an inserted break in such a loop actually would result in in
assemled
code. That was the whole objective of the question.

I'm not interested in answers
trying to tell me that an example I gave to clarify the -structural-
problem is not
a sensible program or is bad code or that I do not follow holy
standards. It's not
about the code, the question is about what an inserted break in any
loop (that is
not optimized away of course in the real program, I'm not that stupid,
but I assumed
some smartness of the reader here) actually inserts in the assembled
code.
Actually, to just give you an example, not even all code in K&R is
sensible code, but allas.

Since I will not and cannot display to you the full code I'll find out
myself what
is happening. Anyway, thanks for the fish and the (few) sensible
answers.

Apr 29 '07 #18
a.***@chello.nl wrote, On 29/04/07 22:38:

<snip>
Boy, oh boy... I thought there would be some intelligent people here
There are plenty of intelligent people.
who would
know what an inserted break in such a loop actually would result in in
assemled
code. That was the whole objective of the question.
That all depends on the processor, the compiler, the options given to
the compiler, and often the surrounding code.
I'm not interested in answers
trying to tell me that an example I gave to clarify the -structural-
problem is not
a sensible program or is bad code or that I do not follow holy
standards. It's not
OK, so you don't care if your code is good, bad, or fundamentally
broken. In that case it should not matter what the compiler does.
about the code, the question is about what an inserted break in any
loop (that is
not optimized away of course in the real program, I'm not that stupid,
but I assumed
some smartness of the reader here) actually inserts in the assembled
code.
Well, since under some conditions the entire loop will be optimised away
if *obviously* depends on the *actual* code and the *real* situation.
Actually, to just give you an example, not even all code in K&R is
sensible code, but allas.
It makes a lot more sense than the code you have shown.
Since I will not and cannot display to you the full code I'll find out
myself what
is happening. Anyway, thanks for the fish and the (few) sensible
answers.
If you refuse to give a sensible example people cannot sensibly guess
what a compiler will do, and in any case the impact will depend on the
compiler, the options it is given, and the processor. For example, on
some processors it will break the pipeline, other processors do not even
*have* a pipeline.
--
Flash Gordon
Apr 29 '07 #19
On Apr 30, 9:38 am, a...@chello.nl wrote:
Boy, oh boy... I thought there would be some intelligent people here
who would know what an inserted break in such a loop actually would
result in in assemled code. That was the whole objective of the question.
This newsgroup does not deal with "assemled code"...
I do not follow holy standards.
.... nor with non-standard programs.
I'm not interested in answers trying to tell me that an example I gave
to clarify the -structural- problem is not a sensible program
Surely you agree that code which divides by zero is not a good example
for anything?

Anyway, it's not clear to me exactly what you are asking, but if you
are
asking about the speeds of varying algorithms, then comp.programming
would be a good newsgroup to ask on.
about the code, the question is about what an inserted break in any
loop actually inserts in the assembled code.
That would depend on your compiler. Some C environments don't even
have any "assembled code". You should post this question on a
newsgroup or forum relevant to your specific compiler.
Run this without the break statement and with the break statement
and look at execution time. On my 1.5GHz P4 it takes 1.3s and
13.9s respectively (!).
Different algorithms take different lengths of time? Who'da thunk it!

It's pretty obvious that your compiler is performing some sort of
optimization, and when you change the code you get a different
optimization. Without any further information it's impossible to
provide a more accurate 'answer'. You should post on a newsgroup
relevant to your specific compiler.
Apr 29 '07 #20
On Apr 29, 2:38 pm, a...@chello.nl wrote:
christian.bau schreef:
On Apr 29, 8:34 am, a...@chello.nl wrote:
It's not my objective to show you the full code; it's about the issue
of execution speed and "break".
Your first example was pure nonsense invoking undefined behavior at
the first step.
Your second example was a completely useless piece of code, where huge
portions of the code could be optimised away. All that you have been
testing is the capability of the compiler to detect nonsense code and
remove it. It seems your break statement affected the compilers
capability of detecting nonsense code. This might be of importance to
you, it is of no importance to those 99.9999 percent of programmers
who actually write more sensible code in the first place.

Boy, oh boy... I thought there would be some intelligent people here
who would
know what an inserted break in such a loop actually would result in in
assemled
code. That was the whole objective of the question.
Then you have very strange thought processes, or a complete
misunderstanding of how C compilers work. It obviously depends
entirely on at least the complete compilation unit, the compiler, the
target processor, and the options given to the compiler. If your code
isn't correct C, it could also depend on the phase of the moon or
anything else. The only thing which can tell you is the compiler, and
the smallest change to anything in the list above could change its
behaviour entirely.
I'm not interested in answers
trying to tell me that an example I gave to clarify the -structural-
problem is not
a sensible program or is bad code or that I do not follow holy
standards. It's not
about the code, the question is about what an inserted break in any
loop (that is
not optimized away of course in the real program, I'm not that stupid,
but I assumed
some smartness of the reader here) actually inserts in the assembled
code.
How the Hell could anyone here know, without knowledge of the entire
compilation unit and detailed knowledge of your particular compiler's
algorithms under all the conditions you're providing? It can do
anything it likes as long as the code behaves in the way that C
defines it should behave, and if the code is not valid C then all bets
are off.

Apr 29 '07 #21
a.***@chello.nl writes:
christian.bau schreef:
>On Apr 29, 8:34 am, a...@chello.nl wrote:
It's not my objective to show you the full code; it's about the issue
of execution speed and "break".

Your first example was pure nonsense invoking undefined behavior at
the first step.

Your second example was a completely useless piece of code, where huge
portions of the code could be optimised away. All that you have been
testing is the capability of the compiler to detect nonsense code and
remove it. It seems your break statement affected the compilers
capability of detecting nonsense code. This might be of importance to
you, it is of no importance to those 99.9999 percent of programmers
who actually write more sensible code in the first place.

Boy, oh boy... I thought there would be some intelligent people here
who would know what an inserted break in such a loop actually would
result in in assemled code. That was the whole objective of the
question.

I'm not interested in answers trying to tell me that an example I
gave to clarify the -structural- problem is not a sensible program
or is bad code or that I do not follow holy standards. It's not
about the code, the question is about what an inserted break in any
loop (that is not optimized away of course in the real program, I'm
not that stupid, but I assumed some smartness of the reader here)
actually inserts in the assembled code. Actually, to just give you
an example, not even all code in K&R is sensible code, but allas.
C99 6.8.6.3p2:

A break statement terminates execution of the smallest enclosing
switch or iteration statement.

That is quite simply *all* we can tell you here. If you're asking
about what assembly or machine code will be generated, that's not a
question about C, and we can't answer it.
Since I will not and cannot display to you the full code I'll find
out myself what is happening. Anyway, thanks for the fish and the
(few) sensible answers.
The code you deleted when you posted your example was probably
critical to answering your question. I'm not sure what you expected
us to tell you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 30 '07 #22

Hmm, at least now I get sensible answers :)

To give a short corrolary: I turned off optimization, assured myself
the loop made sense and could not be "optimized" away (if any
optimization at all) but still the "break" made the code slower than
without.
Anyway, I got some good ideas through this interaction, so thanks for
the fish And the chips!

Apr 30 '07 #23
In article <11**********************@h2g2000hsg.googlegroups. com>,
<a.***@chello.nlwrote:
>To give a short corrolary: I turned off optimization, assured myself
the loop made sense and could not be "optimized" away (if any
optimization at all) but still the "break" made the code slower than
without.
If you really want to know what's happening, look at the assembly
code. Most compilers can show it to you (for example, on unix systems
"cc -S" will generate a .s file containing the assembly code). Even
if you're not familiar with the processor's assembly language, you can
usually recognise the loops. Compare the output with and without
optimisation. If you're using GCC, there are hundreds of switches to
disable specific optimisations so you can narrow it down further.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 30 '07 #24
a.***@chello.nl wrote:
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??
The only ones who can give a definitive answer to that question are the
authors of your compiler (or rather, its optimiser).
Isn't it just an extra "jmp" in assembler equivalent code?
Before the optimiser gets his hands on the code, that (or whatever
equivalent the compiler uses internally) is probably what gets
generated for the break statement.
I compared
both assembled outputs with and without the break statement, but they
really look quite different.. ?! I optimize with -O3 using gcc.
But all kinds of strange stings will happen to the code when the
optimiser tries to squeeze every last microsecond out of it.
What most likely happened is that with the extra break statement, the
optimiser did no longer recognise the loop as something it could work
with. Therefor, the loop was no longer being optimised.

The first rule you should learn from this is: Do not try to out-smart
your compiler. You will likely end-up with worse results.
Optimisers typically work best with idiomatically written code, because
that will give the best results for the largest percentage of
customers/users.
Therefor, the basic rules for writing the fastest code are:
1. Write clear, understandable code. Do not try to outsmart your
compiler.
2. If the result is not fast enough, profile the code, to see where the
bottlenecks are.
3. Try to find a more efficient algorithm for each bottleneck.
4. Only if you have the most efficient algorithm, start looking at the
implementation.
5. When you make a change, verify that it is actually an improvement. If
not, revert the change.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
Apr 30 '07 #25
On Apr 29, 10:38 pm, a...@chello.nl wrote:
Boy, oh boy... I thought there would be some intelligent people here
who would
know what an inserted break in such a loop actually would result in in
assemled
code. That was the whole objective of the question.
I'm not interested in answers
trying to tell me that an example I gave to clarify the -structural-
problem is not
a sensible program or is bad code or that I do not follow holy
standards. It's not
about the code, the question is about what an inserted break in any
loop (that is
not optimized away of course in the real program, I'm not that stupid,
but I assumed
some smartness of the reader here) actually inserts in the assembled
code.
Doesn't look to me as if you are the brightest bulb, and your disdain
for "holy standards" is what bites you in the arse.

Your code is pointless: Not from any aesthetical point of you (like a
program printing the prime numbers up to one million could be called
pointless) but from the point of view of the C language: A function
that calculates some results but never prints them, never uses them,
never reports them to the outside world, is pointless on the level of
the programming language and can therefore be optimised away. This is
exactly what happened to your code. And the more optimisations the
compiler uses, the more will it bite you when your view of the C
language is not the same as that of the C standard.

Apr 30 '07 #26
On Apr 29, 8:32 am, Richard Heathfield <r...@see.sig.invalidwrote:
a...@chello.nl said:
Ok, this was a bad fast example, I admit.
Therefore below another example that shows the point.
int main()
{
unsigned long i,j,k,l,n;
for (l=0;l<10000000;l++) {
j=0;
k=10;
n=231;
for (i=1;n>3;j+=8,k+=j,n-=2) {
if (k%n) continue;
i=0;
break;
}
}
return 0;
}
Run this without the break statement and with the break statement and
look at execution time. On my 1.5GHz P4 it takes 1.3s and 13.9s
respectively (!). My store on the factor 100 incorporates a lot more
code.

The same result can be achieved much more quickly:

int main(void)
{
return 0;

}
Why do optimizations like that by hand when the compiler can do them
for you? :)
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

May 15 '07 #27

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jay | last post by:
G'day all This registration form checks for the submit button then displays the next form from the include statement. But before it displays the next form it will check to make sure the user...
5
by: Glen Wheeler | last post by:
Hello All. I've been coding in python for a reasonable amount of time now (coding in total for approx. 20 years) and am writing a performance intensive program in python. The problem I'm having...
5
by: Ann | last post by:
I have trouble sometimes figuring out where break and continue go to. Is there some easy way to figure it out, or a tool? TIA Ann
1
by: mhk | last post by:
Hi, i have a nested loop in Javascript and i have a break statement in inner loop. As break statement takes control come out of inner loop. I want to come out of both loops if break statement...
7
by: Colin King | last post by:
Amusingly, one can use a while(0) statement to allow one to perform a switch statement without breaks. The while (0) enables the continue statements to break out of the switch. Ugly and...
3
by: Krish | last post by:
Hello, This is an issue that I have encountered several times but have just used a workaround to solve. What is the accepted practice for breaking out of nested loops? For example: while(...
6
by: David | last post by:
I know that by some reasons... the use of "break;" in java language is not correct, is there any similar problems with c#????
2
by: Sorin Schwimmer | last post by:
I am thinking on something in the following form: <code> import time import thread delay=True def fn() global delay
26
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.