不通过引用传递简单类型的原因? [英] Reasons to not pass simple types by reference?

查看:97
本文介绍了不通过引用传递简单类型的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,你不应该通过引用在c ++中传递简单的类型,因为它不会提高性能,它甚至不利于性能(?)。至少这是我从网上收集的。

So far as I understand you should not pass simple types by reference in c++ because it does not improve perfomance, it's even bad for performance(?). At least thats what I managed to gather from the net.

但我找不到 reason 为什么它对性能有害,因为c ++只是创建一个新的简单类型比查找变量或它是什么更快?

But I can't find out the reason why it's bad for performance, is it because it's quicker for c++ to just create a new simple type than it it is too look up variable or what is it?

推荐答案

如果你创建一个引用,它是:

If you create a reference, it's:

pointer to memory location -> memory location

如果使用值,则为:

memory location

复制任何一种方式(引用或值),通过引用传递不会提高性能;必须进行一次额外的查找。所以它理论上恶化的表现,但不是任何金额你会注意到。

Since a value has to be copied either way (the reference or the value), passing by reference does not improve performance; one extra lookup has to be made. So it theoretically "worsens" performance, but not by any amount you'll ever notice.

这篇关于不通过引用传递简单类型的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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