Performance, for(int i...) vs for(i...)
Tim Prince
n8tm@aol.com
Sun Aug 26 18:08:00 GMT 2012
On 08/26/2012 11:30 AM, lab@kayohamid.net wrote:
> 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?
I expect the distinction to be important only where local scope is
wanted, for example in parallel constructs such as OpenMP and cilk_for.
It's probably important to continue to handle the declaration outside
the scope well, as that version is required for C source code by
Microsoft compilers, even VS2012.
I have had occasion to wish that people didn't rely on non-standard
usage for the case of a break; maybe Microsoft is right in forbidding
both incorrect and correct C++ usage in C code.
--
Tim Prince
More information about the Gcc-help
mailing list