[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Program flow, loops and speed

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*/
Loop:
//... 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.

Thanks,
blackwolf359
Dustin Juliano
0
blackwolf359
Asked:
blackwolf359
1 Solution
 
limestarCommented:
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:

label:
...
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.
0
 
blackwolf359Author Commented:
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)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now