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)?

Recommended Answers

All 8 Replies

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

Well at least you didn't mention premature optimization.

Well at least you didn't mention premature optimization.

not explicitly, but it was certainly implied.

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

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.

>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.

Go look at the generated assembly.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.