Program flow, loops and speed

Posted on 2003-03-15
Medium Priority
Last Modified: 2010-04-01
Hello all,

I am an experienced programmer.  I already have an idea or theory as to what the answer is to this question, but, I am not afraid to poll the audience to see what everyone else thinks.

This question is about loops.  *Which is faster?*

1.  for (;;)
2.  while (1)

or 3:

/* code snippet start*/
//... perform instruction
goto Loop;
/* end code snippet */

I would ask to satify my question that you provide which one, and why.  Why being timing issues, how many instructions per cycle, or some other profiling tool or timing tool you used.

Dustin Juliano
Question by:blackwolf359
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

limestar earned 500 total points
ID: 8144787
None, or all. They all compile to exactly the same thing. Try it and you'll see, at least gcc can be made to output asm code for what you compile. I once did an experiment very similar to your question which convirmed what common sense says: They all compile to exactly the same thing because for loops are just syntactic sugar for while loops and while loops are just easier ways to write goto loops.

Anyway, if you want the proof, gcc can give it to you. it looks something like:

jmp label

but of course in a different way depending on what system the assembly language you get out is for. The instruction count for the looping only is 1 in any case.

Author Comment

ID: 8144834
Excellent.  However I never did take it to the level of dumping the asm code and inspecting it or dissasembling it.  I guess I could have ran it in my debugger and turned off the source browsing and saw it for myself.

Thanks, I'll continue to use good ol while(1)

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question