Is there any performance difference between these two different programs?
#define K 50
void main() {
int k = K;
}
void main() {
int k = 50;
}
CodePudding user response:
Macros in C perform token substitution before the actual compilation stage.
This means that after preprocessing, the second piece of code is exactly the same as the first.
CodePudding user response:
In this case: No, it makes 0 difference (not even at -O0!).
This is the assembler gcc 10 on ubuntu 20.04 produced on my machine:
.file "compare_assign.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
endbr64
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $50, -4(%rbp)
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0"
.section .note.GNU-stack,"",@progbits
.section .note.gnu.property,"a"
.align 8
.long 1f - 0f
.long 4f - 1f
.long 5
0:
.string "GNU"
1:
.align 8
.long 0xc0000002
.long 3f - 2f
2:
.long 0x3
3:
.align 8
4:
Only difference is the title of the source file (duh):
diff compare_assign_noopt.asm compare_define_noopt.asm
1c1
< .file "compare_assign.c"
---
> .file "compare_define.c"