传递对输出位置的引用与使用返回
当调用提供简单数据类型的函数时,哪个对性能更好——让它填充内存位置(通过指针传递)还是让它返回简单数据?
我在这里过度简化了返回静态值 5 的示例,但假设确定返回值的查找/功能在现实生活中是动态的...
传统逻辑会告诉我第一种方法更快,因为我们通过引用进行操作而不是像第二种方法那样必须返回副本......但是,我想听听其他人的意见。
谢谢
<代码> 无效函数(int *a){ *a = 5; }
或...
int 函数() { 返回 5; }
Which is better for performance when calling a function that provides a simple datatype -- having it fill in a memory location (passed by pointer) or having it return the simple data?
I've oversimplified the example returning a static value of 5 here, but assume the lookup/functionality that determines the return value would be dynamic in real life...
Conventional logic would tell me the first approach is quicker since we are operating by reference instead of having to return a copy as in the 2nd approach... But, I'd like others' opinions.
Thanks
void func(int *a) {
*a = 5;
}
or...
int func() {
return 5;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一般来说,如果您的函数的行为类似于函数(即返回单个逻辑值),那么最好使用 int func()。即使返回值是复杂的 C++ 对象,也有一种称为返回值优化的常见优化,可以避免不必要的对象复制,并使两种形式在运行时性能上大致相当。
In general, if your function acts like a function (that is, returning a single logical value), then it's probably best to use
int func()
. Even if the return value is a complex C++ object, there's a common optimisation called Return Value Optimisation that avoids unnecessary object copying and makes the two forms roughly equivalent in runtime performance.大多数编译器都会在寄存器中返回一个值,只要您返回的值足够小以适合寄存器。其他任何东西都比这更有效,这是非常不寻常的(而且通常几乎不可能)。
Most compilers will return a value in a register as long as what you're returning is small enough to fit in a register. It's pretty unusual (and often nearly impossible) for anything else to be more efficient than that.
对于 POD,没有或几乎没有区别,我总是选择返回值,因为我发现它们更干净、更容易阅读。
对于非 POD,答案是“这取决于”——许多编译器在这种倾向于创建隐式引用参数的场景中使用返回值优化。
但是,除非您已经测量过(不是“知道”,而是使用分析器实际测量过),否则使用返回值返回函数结果实际上是软件中的瓶颈,否则请选择更具可读性的代码版本。
For PODs, there is no or almost no difference and I'd always go with a return value as I find those cleaner and easier to read.
For non-PODs the answer is "it depends" - a lot of compilers use Return Value Optimisation in this sort of scenario which tends to create an implicit reference parameter.
However unless you have measured - not "know", but actually measured with a profiler - that returning the results of the function using a return value is actually a bottleneck in your software, go for the more readable version of the code.
在我看来,总是使用
return
,除非您知道不这样做的原因,或者您必须从函数返回多个值。返回内置类型非常有效,并且无论与通过指针返回有什么区别,它都必须可以忽略不计。但这里真正的好处是使用return
对于那些稍后阅读代码的人来说更清晰、更简单。In my opinion, always go with
return
unless you know of a reason not to, or you have to return more than one value from the function. Returning a built-in type is very efficient, and whatever the difference vs. returning via pointer, it must be negligible. But the real benefit here is usingreturn
is clearer and simpler for those who read the code later.返回一个简单的值就像汇编中的指令(即 MOV eax,xxxx )一样,传递参数会引入更多的开销。无论如何,您不必担心这一点,差异很难注意到。
另一个重要的问题是,左侧的函数返回通常在设计上更简洁,并且在可能的情况下是首选。
Returning a simple value is just something like an instrution in assmbly ( ie MOV eax,xxxx ), passing a parameter introduce a little more overhead. in any case you should not worry about that, difference are hard to notice.
Another important issue is that a function returniong on the left is generally cleaner in term of design, and preferred when possible.
这是一个低层次的事情,很难看出任何差异。
This is a low level thing, where it would be hard to see any difference.
简单回答:这取决于。
这取决于所使用的类型,是否可以廉价地复制它们(或根本无法复制),编译器是否可以在某些情况下使用 RVO,以一种或另一种形式更好地内联事物......
使用有意义的内容上下文。
Easy answer: it depends.
It depends on the types being used, whether they can be copied cheaply or not (or at all), whether the compiler can use RVO in some circumstances or not, inline things better with one form or another...
Use what makes sense in the context.