zoukankan      html  css  js  c++  java
  • C++: Why pass-by-value is generally more efficient than pass-by-reference for built-in (i.e., C-like) types

    A compiler vendor would typically implement a reference as a pointer. Pointers tend to be the same size as or larger than many of the built-in types. For these built-in types the same amount of data would be passed whether you passed by value or by reference. In the function, in order to get the actual data, you would however need to dereference this internal pointer. This can add an instruction to the generated code, and you will also have two memory locations that may not be in cache. The difference won't be much - but it could be measured in tight loops.

    A compiler vendor could choose to disregard const references (and sometimes also non-const references) when they're used on built-in types - all depending on the information available to the compiler when it deals with the function and its callers.

  • 相关阅读:
    as
    留言板
    HEOI2020游记
    min_25筛学习笔记
    计算几何初步
    「狗屁不会」exlucas
    GCD of Sequence
    做题记录
    一些奇怪的坑+好东西
    关于我
  • 原文地址:https://www.cnblogs.com/DebugMe/p/3167045.html
Copyright © 2011-2022 走看看