将对象作为参数传递时,它们是通过引用传递的吗? [英] When passing objects as parameters, are they passed by reference?

查看:73
本文介绍了将对象作为参数传递时,它们是通过引用传递的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我执行以下任一操作,是否对性能有任何影响:

Is there any performance implications if I do either of these:

def do_something(user, article)
...
end

对比

def do_something(user_id, article_id)
  ..
end

我更喜欢传递对象,因为我可能需要其他属性.

I prefer passing objects as I might need other attributes down the road.

推荐答案

两个方法调用将花费大致相同的时间.

Yes

Both method calls will take about the same amount of time.

(了解性能后果并且您提出了一个合理的问题是件好事,但即便如此,关于早期优化的标准免责声明1在技术上仍然适用.)

(It's good to be aware of performance consequences and you asked a reasonable question, but even so, the standard disclaimer1 about early optimization technically applies.)

1.首先,使程序工作.然后,简介.最后,也许,优化.
Donald Knuth 说:我们应该忘记小效率,说大约 97%时间:过早的优化是万恶之源.

这篇关于将对象作为参数传递时,它们是通过引用传递的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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