這兩個不同的程式之間有什么性能差異嗎?
#define K 50
void main() {
int k = K;
}
void main() {
int k = 50;
}
uj5u.com熱心網友回復:
C 中的宏在實際編譯階段之前執行標記替換。
這意味著經過預處理后,第二段代碼與第一段完全相同。
uj5u.com熱心網友回復:
在這種情況下:不,它產生 0 差異(甚至在 -O0 處也不行!)。
這是在我的機器上生成的 ubuntu 20.04 上的匯編器 gcc 10:
.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:
唯一的區別是源檔案的標題(廢話):
diff compare_assign_noopt.asm compare_define_noopt.asm
1c1
< .file "compare_assign.c"
---
> .file "compare_define.c"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/366016.html
