In PHP ( = 5.0), is passing by reference faster?
The Zend Engine uses copy-on-write, and when you use a reference yourself, it incurs a little extra overhead. Can only find this mention at time of writing though, and comments in the manual contain other links.
(EDIT) The manual page on Objects and references contains a little more info on how object variables differ from references.
Benefit of passing by reference? (php)
Passing by reference is faster. PHP5 do pass objects by reference by default. I think under PHP 5.3, you still have to do $obj = &new Object();
, but I could be wrong about that.
PHP5 do not pass array by reference. If you want to modify them in a function, you need to pass by reference.
Passing by value means that every single value is copied. For example, if you pass an array by value, it copies the array to a different memory location and every single element in it.
Why is calling a function (such as strlen, count etc) on a referenced value so slow?
I found a bug report from 2005 that describes exactly this issue: http://bugs.php.net/bug.php?id=34540
So the problem seems to be that when passing a referenced value to a function that doesn't accept a reference, PHP needs to copy it.
This can be demonstrated with this test code:
<?php
function CalledFunc(&$aData)
{
// Do nothing
}
function TestFunc(&$aArray)
{
$aArray = range(0, 100000);
$fStartTime = microtime(true);
for ($iIter = 0; $iIter < 1000; $iIter++)
{
CalledFunc($aArray);
}
$fTaken = microtime(true) - $fStartTime;
print "took $fTaken seconds\n";
}
$aArray = array();
TestFunc($sData);
?>
This runs quickly, but if you change function CalledFunc(&$aData)
to function CalledFunc($aData)
you'll see a similar slow-down to the count
example.
This is rather worrying, since I've been coding PHP for quite a while and I had no idea about this issue.
Fortunately there's a simple workaround that is applicable in many cases - use a temporary local variable inside the loop, and copy to the reference variable at the end.
passing a string by reference to a function would speed things up? (php)
Don't think, profile.
Run scripts that use each version of the function for 100,000 repetitions under, say, the Unix time
command. Do not philosophize about what is faster; find out.
Objects are passed by reference. Parameters to call_user_func aren't. What gives?
Parameters passing
The main issue is - that parameters, passed to call_user_func()
will be passed as values - so they will be copy of actual data. This behavior overrides the fact, that
objects are passed by reference. Note:
Note that the parameters for call_user_func() are not passed by
reference.
Tracking error
You're not fully correct about "silent agreement" in such cases. You will see error with level E_WARNING
in such cases:
Warning: Parameter 1 to with_ref() expected to be a reference, value given in
So - you will be able to figure out that you're mixing reference and values passing
Fixing the issue
Fortunately, it's not too hard to avoid this problem. Simply create reference to desired value:
class Example {
function RunEvent($event) {
if (isset($this->events[$event])) {
foreach ($this->events[$event] as $k => $v) {
$obj = &$this;
call_user_func($v, $obj);
}
}
}
}
-then result will be quite as expected:
object(Example)#1 (3) {
["events"]=>
array(1) {
["example"]=>
array(2) {
[0]=>
string(8) "with_ref"
[1]=>
string(11) "without_ref"
}
}
["with_ref"]=>
bool(true)
["without_ref"]=>
bool(true)
}
Does Kotlin allow for `optionally` passing by reference like the & in php?
I believe that similar to Java, Kotlin is always pass-by-value and optional pass-by-reference is not possible.
PHP does lots of type checks slow down your code?
You can find out yourself.
Store the current time in milliseconds at the start of your script, run a loop which does a type check several thousand times, and compare the time at the end of your script with the time at the start of your script.
Related Topics
List All Files in One Directory PHP
Blocking Comment Spam Without Using Captcha
How to PHP-Unserialize a Jquery-Serialized Form
Disable PHP in Directory (Including All Sub-Directories) With .Htaccess
What's the Net::Err_Http2_Protocol_Error About
Facebook Graph API, How to Get Users Email
How to Run a PHP File in a Scheduled Task (Windows Task Scheduler)
PHP Sort Array by Two Field Values
Include PHP Inside JavaScript (.Js) Files
PHP to Search Within Txt File and Echo the Whole Line
Error Message Strict Standards: Non-Static Method Should Not Be Called Statically in PHP
How to Remove Both .PHP and .Html Extensions from Url Using Nginx