954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

are while-loops compiled efficiently?

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

laehc
Newbie Poster
11 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

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

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

Well at least you didn't mention premature optimization.

laehc
Newbie Poster
11 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 
Well at least you didn't mention premature optimization.

not explicitly, but it was certainly implied.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

laehc
Newbie Poster
11 posts since Mar 2010
Reputation Points: 10
Solved Threads: 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Go look at the generated assembly.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 
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.

laehc
Newbie Poster
11 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You