What is the efficiency of a PHP function whose parameters are passed and referenced by value?
Pass by value
Function ABC ($t) {
$c = $t;
}
Reference delivery
Function ABC (& $t) {
$c = $t;
}
I assign a long article to the variable $t, and then loop 100,000 times ABC ($t), and find that the rate of passing by value is much faster than the reference pass.
What I don't understand is why is it faster to pass the value in PHP instead? In theory, there is a copy of the process by value, and the reference is directly to the memory address, should refer to delivery faster, I hope you can answer my question.
In addition, what to say about PHP performance efficiency of the book, recommended, thank you
------to solve the idea----------------------
In PHP, the principle of "copy-on-write" for the & reference operator is that a variable or object that points to the same address will not be copied unless a write operation occurs.
When you pass a $ A array into a function using the & symbol, the PHP engine will think that this function may cause a change to $ A, and it will automatically produce a $ A copy of the data for $b and re-request a piece of memory for storage. This is the "copy-on-write" concept mentioned earlier.
You can test comparisons with value passing and reference delivery execution efficiency, such as adding a loop 1000 times outside to see how time is running, and the result will let you know that incorrect use of PHP references & symbols can result in performance degradation of more than 30%.
Because more than one copy of the operation, it will lead to the cost of system resources (especially the case of large arrays), so in PHP, as far as possible to use the value of the pass, and less by reference.
------to solve the idea----------------------
So we need to learn constantly.
by # #
1,255,441 start of memory consumption
126640 after defining the $a, an increase of 1096
f1:126656 call F1, increased by 16 (should be a pointer to $ A or something)
f1:126688
126656 after returning from F1, there are still 16 not released
f2:126672 call F2, increased by 16
f2:125656
125656 memory possession restored after returning from F2 (126656)
Can see: The previous data on the creation of copies, write-time copies, etc. are obsolete!