Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While Loop vs For Loop, which is more memory efficient!

Tags:

java

c

Now that I am used to programming in very memory constraint conditions, one question which I dont have an answer is : Which is more memory efficient;- for(;;) or while() ? Or are they interchangeable equally? Also comment on efficiency matters if any!

like image 666
Purushottam Avatar asked Dec 17 '22 08:12

Purushottam


2 Answers

There is no difference in memory use between the programming language construct for and while. A given compiler might optimize one or the other better but you should not worry about this. With memory constraint conditions the most important thing is going to be your data structures and system design. A poor system design could require more memory.

like image 180
Hogan Avatar answered Dec 18 '22 22:12

Hogan


The only way to be sure is to test it with your compiler for your platform. But I suppose any sufficiently advanced optimizing C compiler would optimize them to the same thing in most cases.

like image 31
cyco130 Avatar answered Dec 18 '22 22:12

cyco130