0

The obvious way to compile a while-loop is with a conditional branch at the start and an unconditional branch at the end to loop back. Suppose the condition is A>B, then a conditional branch at the end testing A<=B will fall through when the loop is done, instead of jumping back, testing A>B and then jumping to the code following the loop.

I ran a test using gfortran (because Fortran allows goto) and my test code using 'do while's ran about 7 or 8% slower than equivalent code using an 'if' and a 'goto' at the start of the loop, and another 'if' and 'goto' at the end of the loop. The code inside the loop just adds or subtracts 1 to a count, to provide a check that the two versions really are equivalent (rather oddly, the result was quite often 42).

Both versions had 6KB object files, presumably if a source had enough loops then the double-conditional version would be detectably bigger.

Is the difference in speed what you would expect?
Would you usually expect a compiler to only put a conditional branch at the start of a while-loop (in any language)?

5
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by laehc
1

You should not worry about how fast a fundamental loop is. If you
care about speed, then your bottleneck is probably elsewhere.

0

Well at least you didn't mention premature optimization.

0

>Well at least you didn't mention premature optimization.
I suspect you get that a lot. Probably for good reason.

-1

No-one has personally quoted Knuth or Sir Charles Antony Richard Hoare at me yet, but I come across the quote quite often while browsing, usually in a less considered form than the original.

I conformed to the required guidelines when I was employed as a programmer, but I don't see why I shouldn't be a little curious about how compilers compile while loops, in my spare time.

I regret having bothered the forum with such a controversial subject.

0

>I don't see why I shouldn't be a little curious about
>how compilers compile while loops, in my spare time.

I don't either. Personally, I'd love to talk about the inner workings of compilers with someone interested in the subject. But it wasn't clear that you're generally interested in the machine code produced by compilers. Rather, it seemed like you were counting cycles and then asking us if your performance tests were "expected" across all compilers of all languages. :icon_rolleyes:

>I regret having bothered the forum with such a controversial subject.
It's not controversial at all, and if you're going to be a whiny baby after a couple of people misinterpret your misleading question, I don't regret not answering it.

0

Go look at the generated assembly.

Thanks Rashakil Fol. I was about to take your advice, then I realised I was probably wrong about it being a compiler issue, and found this link. It explains loop inversion with an example in C.
http://en.wikipedia.org/wiki/Loop_inversion
My Fortran 'goto's were not a good way to do it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.