Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best infinite loop [duplicate]

Possible Duplicate:
while (1) Vs. for (;;) Is there a speed difference?

Hi,

Which is better,faster and more optimized way to implement infinite loop - for(;;) or while(1)? and why?

like image 695
iSegFault Avatar asked Aug 17 '10 18:08

iSegFault


1 Answers

I prefer for(;;) because it doesn't test anything and semantically this is what you mean. It doesn't make so much sense to keep testing if 1 is true. However any professional C programmer should immediately recognize both idioms as both are used.

In terms of actual performance there should be no difference. The compiler will optimize away the test.

I tried testing both to see which is faster but neither of them has completed yet.

like image 181
Mark Byers Avatar answered Oct 23 '22 14:10

Mark Byers