Which of the following is more efficient for passing PHP function parameters by value or by reference?

Source: Internet
Author: User
Which of the following is more efficient for passing PHP function parameters by value or by reference? Pass by value
function abc($t){$c=$t;}

Reference transfer
function abc(&$t){$c=$t;}

I assigned a long article to the variable $ t, and then cyclically made it 0.1 million times abc ($ t). I found that passing by value is much faster than transferring by reference.

What I cannot understand is why passing by value in PHP is faster? Theoretically, there is a replication process for passing by value, and the reference is directed directly to the memory address. it should be faster to transfer the reference. I hope you can answer my questions.

What other books about PHP execution efficiency are recommended? thank you.


Reply to discussion (solution)

Your test method should be problematic, so the test results are of no value.

The difference between passing a value and passing a reference is that the former cannot be reflected in the changes to it in the function body, while the latter can

The memory consumption is the same for transferring values and transferring references.
When transferring values, you need to copy the copy before calling the function.
When a reference is passed, an empty variable body with the reference mark is first transmitted to the function, and the copy is copied when the variable is used in the function body.

The variable body of php is a 64-byte structure, so no matter whether the reference is passed or not, the size of the struct will not become

Whether to pass the value or reference depends on the control logic and personal preferences.
It is easier to transmit references when multiple variables are returned at the same time.

When a reference is passed, the passed variable is like a semi-finished product on the pipeline. each function only processes a part of the variable.
It looks smoother in form, and you can determine whether the operation is successful through the return value of the function (what design mode is this? Do not remember)

It is actually the same,
However, if you want to know the value after modification, you can use the reference. Otherwise, you can use the value.

Your test method should be problematic, so the test results are of no value.

The difference between passing a value and passing a reference is that the former cannot be reflected in the changes to it in the function body, while the latter can

The memory consumption is the same for transferring values and transferring references.
When transferring values, you need to copy the copy before calling the function.
When a reference is passed, an empty variable body with the reference mark is first transmitted to the function, and the copy is copied when the variable is used in the function body.

The variable body of php is a 64-byte structure, so no matter whether the reference is passed or not, the size of the struct will not become

Whether to pass the value or reference depends on the control logic and personal preferences.
It is easier to transmit references when multiple variables are returned at the same time.

When a reference is passed, the passed variable is like a semi-finished product on the pipeline. each function only processes a part of the variable.
It looks smoother in form, and you can determine whether the operation is successful through the return value of the function (what design mode is this? Do not remember)

Why do I still have a copy operation when I upload a reference?

Well, the knowledge is a little old.

echo memory_get_usage(), PHP_EOL;$a = str_repeat('*', 1000);echo memory_get_usage(), PHP_EOL;f1($a);echo memory_get_usage(), PHP_EOL;f2($a);echo memory_get_usage(), PHP_EOL;function f1($a) {  echo __FUNCTION__, ':', memory_get_usage(), PHP_EOL;  $a += ' ';  echo __FUNCTION__, ':', memory_get_usage(), PHP_EOL;}function f2(&$a) {  echo __FUNCTION__, ':', memory_get_usage(), PHP_EOL;  $a += ' ';  echo __FUNCTION__, ':', memory_get_usage(), PHP_EOL;}
125544126640f1:126656f1:126688126656f2:126672f2:125656125656

In PHP, the & reference operator adopts the "copy upon write" principle, that is, unless a write operation occurs, the variables or objects pointing to the same address will not be copied.

When you use the & symbol to pass the $ a array into the function, the PHP engine will think that this function may cause changes to $, at this time, a $ a data copy is automatically generated for $ B, and a memory is re-applied for storage. This is the "copy at write time" concept mentioned above.

You can test and compare the execution efficiency of using value transfer and reference transfer, for example, adding a loop 1000 times outside to check the running time, the result will let you know that improper use of PHP references and symbols will lead to a performance reduction of more than 30%.

Because an additional copy operation causes system resource consumption (especially in the case of large arrays), we should try to use value transfer in PHP, instead, use references for transmission.

So we need to keep learning.
By #4
125544 memory usage at the beginning
126640 adds 1096 after $ a is defined
F1: 126656 calls f1 and increases by 16 (it should be something like a pointer pointing to $)
F1: 126688.
126656 after the result is returned from f1, 16 remain unreleased
F2: 126672 passed reference call f2, increased by 16
F2: 125656.
125656 after returning from f2, the memory occupied is restored (126656)

We can see that the previous documents mentioned in the document "Create copy" and "copy at Write Time" are outdated!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.