It is efficient. gcc
will optimize that, like most of the modern compilers. Also keep in mind what Donald Knuth said:
We should forget about small efficiencies, say about 97% of the time:
premature optimization is the root of all evil.
You can check that it is efficient by comparing assembly code. Use for example diff
to do the comparison. To generate assembly use -S
flag, gcc -S
. It is equivalent to gcc -S -O0
, because default optimization level is 0. So, even on the lowest level optimizaton, gcc
will take care of this variable for you.
First version (for better readability prefer this way):
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
printf("%d ", i + j);
}
}
return 0;
}
Second version:
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++)
{
int j;
for (j = 0; j < 10; j++)
{
printf("%d ", i + j);
}
}
return 0;
}
Identical assembly result:
.file "main.cpp"
.section .rodata
.LC0:
.string "%d "
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $16, %rsp
movl $0, -4(%rbp)
jmp .L2
.L5:
movl $0, -8(%rbp)
jmp .L3
.L4:
movl -8(%rbp), %eax
movl -4(%rbp), %edx
leal (%rdx,%rax), %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
addl $1, -8(%rbp)
.L3:
cmpl $9, -8(%rbp)
setle %al
testb %al, %al
jne .L4
addl $1, -4(%rbp)
.L2:
cmpl $9, -4(%rbp)
setle %al
testb %al, %al
jne .L5
movl $0, %eax
leave
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3"
.section .note.GNU-stack,"",@progbits
int i
is -4(%rbp)
, int j
is -8(%rbp)
. As you can see int j
is not reallocated or something.