【问题标题】:Does the byte size of an argument affect the overhead of an opaque function call?参数的字节大小是否会影响不透明函数调用的开销?
【发布时间】:2016-01-23 20:05:57
【问题描述】:

最好通过代码示例来询问:

typedef struct {
    ... // Fields take up many bytes (>= 32 bytes)
} some_struct;

void alternative_1(some_struct arg);
void alternative_2(const some_struct *arg);

假设这两种函数替代方案都是在已编译的二进制文件中实现的(因此不能内联),函数调用开销是否存在差异?

【问题讨论】:

    标签: c performance struct function-calls


    【解决方案1】:

    显然是的:

    当调用 alternative_1 时,整个结构数据在分支到函数之前被压入堆栈(可能很多)。

    alternative_2 只有一个指向压入堆栈的结构的指针。

    对struct进行操作也有很大区别:

    • 对于 alternative_1,您在本地副本上工作。
    • 修改 alternative_2 中的结构时,您会更改原始数据。

    它与接收整数指向整数的指针的函数非常相似。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多