PHP传递变量方法的性能差异
在 PHP 中将数据作为值而不是作为引用传递时,是否会有任何可测量的性能差异?
似乎很少有人知道变量可以作为值而不是引用传递。这是常识还是不常识?
Will there be any measurable performance difference when passing data as values instead of as reference in PHP?
It seems like few people are aware of that variables can be passed as values instead of references. Is this common sense or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据我的理解,PHP 5 按值传递简单的数据类型和数组,但当涉及到对象时,它按引用传递。看来这是您应该注意的行为 - 我假设数组是按值传递的,因此如果您不需要创建副本,大数组很可能会导致性能下降。
我见过很多反对显式通过引用传递并让 PHP 完成其任务的争论。
另外,如果您想按值传递对象,那么理想情况下您应该克隆它。
From my understanding, PHP 5 passes simple data types and arrays by value, but when it comes to objects it passes by reference. It seems this is a behaviour you should be aware of - I assume arrays are passed by value and therefore large ones may well incur a performance hit if you do not require a copy to be made.
I've seen plenty of arguments against passing by reference explicitly and letting PHP do its thing.
Also, if you want to pass an object by value then you should clone it, ideally.
如果您按值传递一个大变量(这是除了 PHP5+ 中的对象之外的所有变量的默认值),那么是的,您可能会受到性能影响。
例如,如果用户提交大量 POST 数据,如果您正常地将其传递给函数(也称为按值传递),则必须复制整个数组,这会影响性能。但是,除非您访问的网站规模非常大,否则您可能不会注意到这种点击。
在 PHP 中可以通过引用传递,但肯定不是默认的(除非它是一个对象):您需要添加一个 & 。在变量之前使其按引用传递,否则它只是按值传递(并复制它)。从 PHP5 开始,对象自动通过引用传递,但在 PHP5 之前,您需要显式通过引用传递(即添加 &)
If you are passing a large variable by value (which is the default for everything except objects in PHP5+), then yes, you can take a performance hit.
For example, if the user submits a large amount of POST data, if you were to pass that to a function normally (aka pass by value), the whole array would have to be copied, which would affect performance. However, unless you're on a very large-scale site, you probably won't notice the hit.
Pass by reference is possible in PHP, but certainly not the default (unless it's an object): you need to add an & before the variable to make it pass by reference, otherwise it's just by value (and copies it). As of PHP5, objects are passed by reference automatically, but before PHP5 you need to explicitly pass by reference (ie add the &)
如果存在性能差异,则可以忽略不计。不要担心这些类型的微优化,除非您知道通过引用传递会导致性能下降(除非我无法想象这种情况是真的)。
顺便说一句,人们通常建议不要通过引用传递参数,因为它会鼓励糟糕的设计,就像使用全局变量一样。
不过,我不确定你最后一部分的意思是什么。 PHP 默认按值传递参数。
If there is a performance difference, it's negligible. Don't worry about these sorts of micro-optimizations unless you know that passing by reference is causing a performance hit (except I can't imagine a situation where that is true).
On a side note, people generally advise against passing arguments by reference because it encourages bad design, much like using global variables does.
I'm not sure what you meant by the last part, though. PHP passes arguments by value by default.
如果您使用最新版本的 PHP,则对象始终通过引用传递。至于其他类型,主要关注的是字符串/数组。
对于那些人来说,这取决于。 PHP 的字符串实现使得如果您不修改传递给函数参数的字符串(您只读取/扫描它),它就永远不会被复制。该实现称为“写时复制”。我不确定数组,我需要一些测试来回答这个问题。
除非修改按值传递的字符串参数,否则与按引用传递没有区别。
Objects are always passed by reference if you use a recent version of PHP. As of the other type, the main concern are the strings / array.
For those it depends. PHP's implementation of strings makes that if you don't modify the string you are passing to the function's argument (you only read it / scan it), it never will be copied. The implementation is called "copy-on-write". I'm not sure about array, I'll need some test to answer this.
Unless you modify the passed by value string argument, there will be no difference with the passed by reference.