Reputation: 624
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!
Upvotes: 4
Views: 10970
Reputation: 146
i did some testing in a c program using clock and the results were inconclusive.. so i wrote the code
for(int i=0;i< 100000000; i++){
g+=2;
}
and the code
int i2 = 0;
while(i2 < 100000000){
g+=2;
i2++;
}
upon compilation in dev c++ using gcc the assembly breaks down to exactly the same bytecode...
CPU Disasm;
Address Hex dump Command Comments
0040160C |. C745 E0 00000 MOV DWORD PTR SS:[LOCAL.8],0
00401613 |> 817D E0 FFE0F /CMP DWORD PTR SS:[LOCAL.8],5F5E0FF
0040161A |. 7F 0D |JG SHORT 00401629
0040161C |. 8D45 E4 |LEA EAX,[LOCAL.7]
0040161F |. 8300 02 |ADD DWORD PTR DS:[EAX],2
00401622 |. 8D45 E0 |LEA EAX,[LOCAL.8]
00401625 |. FF00 |INC DWORD PTR DS:[EAX]
00401627 |.^ EB EA \JMP SHORT 00401613
both sets of code disassemble to that exact chunk of assembly so as you can see they are equally just as good. this coupled with the clock tests always coming back as one faster one time and the other faster the other time proves it...
if you want efficient you could find your code in a disassembler and optimize it by hand ;)
for example those loops could be represented like this
CPU Disasm
Address Hex dump Command Comments
00401492 |. 60 PUSHAD
00401493 |. 31DB XOR EBX,EBX
00401495 |. 31C0 XOR EAX,EAX
00401497 |> 81FB FFE0F505 /CMP EBX,5F5E0FF
0040149D |. 7F 05 |JG SHORT 004014A4
0040149F |. 40 |INC EAX
004014A0 |. 40 |INC EAX
004014A1 |. 43 |INC EBX
004014A2 |.^ EB F3 \JMP SHORT 00401497
004014A4 |> 8945 E4 MOV DWORD PTR SS:[LOCAL.7],EAX
004014A7 |. 61 POPAD
you could change the two INC EAX to ADD EAX,2 but that uses 1 more byte i didnt want to lose :)
this optimization runs about twice as fast :)
Upvotes: 2
Reputation: 3045
Well it depends on the compiler... but think of it like this:
while(x){
x++;
}
uses a variable you declared outside of the scope of the loop, meaning you are using data already made for the loop.
a for loop initializes variables just for the loop inside of the loop.
for(int x = 0; x > y; x++{
}
So if you have a variable just sitting around that has a value you can use, a while loop would most likely(depends on compiler) be better for that situation. But if you needed to make variables just for the loop, I would use a for loop.
Hope this helped.
Upvotes: 1
Reputation: 48226
for(init;cond;inc){
//...
}
is (nearly) exactly the same as
init;
while(cond){
//...
inc;
}
the differences are that continue;
in the for also executes the inc;
but you have to do that manually in the while
and the variables declared in the init;
statement is constrained to the for (it introduces a new scope) however this is not the case in the while
this makes for loops easier to use in several cases (take iteration over an array for example) however the compiled code will likely be the same, so the only reason to choose one over the other is readability
note I'm talking mainly about java I don't know the specifics in c well enough to comment on this
Upvotes: 2
Reputation: 4944
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.
Upvotes: 3
Reputation: 70528
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.
Upvotes: 8