生成性能,C++代码和汇编代码

Avi*_*ash 2 c++ optimization performance

我已经阅读了很多关于提高C++和C代码性能的问题.几乎所有答案人员最终都会观察编译器生成的汇编代码.

如果我想了解这种技术,那么最好的资源是什么?

Zan*_*Jie 5

实践是你最好的老师

写一个简单的测试文件:

#include <stdio.h>

int main() {
    printf("Hello %s !\n", "world");
}
Run Code Online (Sandbox Code Playgroud)

然后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
Run Code Online (Sandbox Code Playgroud)

如果你有了解汇编语言困难时,你最好开始从GNU assembly languagelinkers & loaders,也Intel® 64 and IA-32 Architectures Software Developer’s Manual.这是一本很好的参考书.