C ++做内联函数防止复制? [英] C++ do inline functions prevent copying?

查看:188
本文介绍了C ++做内联函数防止复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设编译器事实上内联 foo 在这两个语句之间有性能差异?

Assuming the compiler does in fact inline foo is there a performance difference between these 2 statements?

inline int foo (int val) {
  return val;
}

int main () {

  std::cout << foo(123) << std::endl;

  std::cout << 123 << std::endl;

  return 0; 
}

让我们忽略移动语义和复制elision可能有的任何含义。 >

Let's ignore any implications that move semantics and copy elision might have.

推荐答案

我的编译器(gcc 4.7.2)为两个语句生成几乎相同的代码:

My compiler (gcc 4.7.2) produces nearly identical code for the two statements:

_main:
LFB1018:
        pushq   %rbx
LCFI0:
        movq    __ZSt4cout@GOTPCREL(%rip), %rbx

; std::cout << foo(123) << std::endl;
        movl    $123, %esi
        movq    %rbx, %rdi
        call    __ZNSolsEi
        movq    %rax, %rdi
        call    __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_

; std::cout << 123 << std::endl;
        movq    %rbx, %rdi
        movl    $123, %esi
        call    __ZNSolsEi
        movq    %rax, %rdi
        call    __ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_

        xorl    %eax, %eax
        popq    %rbx
LCFI1:
        ret

唯一的区别是前两个指令的顺序。我已经尝试了它,这个差异似乎与 foo()没有任何关系:如果我重复两行两次,只有最后一个四个语句的指令顺序颠倒。这使我认为这个工件可能与管道优化器或某种性质有关。

The only difference is the order of the first two instructions. I've experimented with it, and this difference doesn't appear to have anything to do with foo(): if I repeat the two lines twice, only the last of the four statements has the instruction order reversed. This makes me think that this artifact probably has something to do with the pipeline optimizer or something of that nature.

这篇关于C ++做内联函数防止复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆