Hello, I like performance, and until last month i follow what I see on
the web to do better coding for performance, like:
- Avoid i++, use ++i and so on.
But now i'm using the assembly (-S) code generated to see if somethings
is correct, and today i'm investigating this:
int i;
for(i = 0;....);
vs
for(int i = 0;...);
I make 2 codes:
----
int main()
{
int i;
for(i = 0; i < 10; i++) {}
}
----
int main()
{
for(int i=0;i<10;i++) {}
}
----
the result of forin.c and forout.c is the same:
.file "forin.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $16, %esp
movl $0, -4(%ebp)
jmp .L2
.L3:
addl $1, -4(%ebp)
.L2:
cmpl $9, -4(%ebp)
jle .L3
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits
Seeing this, can I say that using int inside the for or outside for is
the same? Have no difference on performance?