2012-06-28 95 views
2

我已經閱讀了很多關於提高C++和C代碼性能的問題。幾乎所有的答案都是觀察編譯器生成的彙編代碼。生成的性能,C++代碼和彙編代碼

如果我想了解這種技術,那麼最好的資源是什麼?

+3

使用反彙編? – nhahtdh

+1

我非常懷疑您將能夠做到比現代編譯器更好,並且所有優化功能都已啓用。 –

+0

所以你要求一本關於彙編語言的書嗎? – unkulunkulu

回答

5

實踐是你最好的老師

編寫一個簡單的測試文件:

#include <stdio.h> 

int main() { 
    printf("Hello %s !\n", "world"); 
} 

然後gcc -S test.cpp會給你test.s生成的彙編代碼。如果你願意,可以添加-Ox

.file "test.cpp" 
    .section .rodata 
.LC0: 
    .string "world" 
.LC1: 
    .string "Hello %s !\n" 
    .text 
    .globl main 
    .type main, @function 
main: 
.LFB0: 
    .cfi_startproc 
    pushq %rbp 
    .cfi_def_cfa_offset 16 
    .cfi_offset 6, -16 
    movq %rsp, %rbp 
    .cfi_def_cfa_register 6 
    movl $.LC0, %esi 
    movl $.LC1, %edi 
    movl $0, %eax 
    call printf 
    movl $0, %eax 
    popq %rbp 
    .cfi_def_cfa 7, 8 
    ret 
    .cfi_endproc 
.LFE0: 
    .size main, .-main 
    .ident "GCC: (Debian 4.7.0-13) 4.7.0" 
    .section .note.GNU-stack,"",@progbits 

若對彙編語言困難,你最好從GNU assembly languagelinkers & loaders,也Intel® 64 and IA-32 Architectures Software Developer’s Manual開始。這是一本很好的參考書。