PHP Small research come out and share with you

Source: Internet
Author: User

Little Insights (reference to the wind and snow and related information on the Internet):

differences in reference and replication:

We all know that the quote is actually: $a = 1; $b = $a; changing $a or $b will not affect each other. References are values by address: $a = 1; $b = & $a; Changing any of the other values will change.

But add a why at the end, why is this so?

One of the two mechanisms that you need to understand in PHP is copy on write and change on write:

Copy on write: Copy at write time, apply to variable replication. For example, $a = 1, $a variable is stored in the symbol table, and the value "1" is stored in the zval. That means PHP is stored

variables, each variable name is stored in the symbol table, each symbol corresponds to a zval;zval can be considered as a container, which contains variable values, variable types, and so on.

$a = 1; $b = $a; someone might think that there are two "1" in memory, which is not actually the case, $a and $b point to the Zval container that holds 1. Can be likened to, $a and $b each have a line point

There is a parameter in the zval,zval that is refcount to count, that is, how many lines point to their own. RefCount adds 1 When a variable is replicated. namely: $a = 1; $b = $a;

The RefCount value in the 1 zval structure is 2. PHP has a function to view, is Debug_zval_dump ($v), when Debug_zval_dump ($a), when the count is 3, because $a is

The value that is passed to Debug_zval_dump is worth 3.

Then the problem is unset, which is to reduce the number of refcount of the variable by one. That is, when the value of the RefCount is 0, the value of the variable will be released from memory. When unset ($b)

1 is not released in memory, but instead points to its reference number minus.

The memory release may not be released to the system (OS), PHP will open up a specified size of memory, when the variable generated will go to their own memory area to apply, when the release will be released to the self

Own memory area inside. has been a great help in improving efficiency. But when it is worth the time to go to the system to apply for memory, it will be released to the system when it is released.

For reference, Zval has another parameter is_ref to mark (tag is a true reference), which defaults to 0, for example: $a = 1; $b = & $a; At this time debug_zval_dump ($a); The result is 1.

RefCount = 2,is_ref = 3,

Version 5.2.17 source of the Zval consortium, you can go to the official website download

typedef Union _ZVALUE_VALUE {

Long lval; /* Long Value * *

Double Dval; /* Double Value * *

struct {

Char *val;

int Len;

} str;

HashTable *ht; /* Hash Table value * *

Zend_object_value obj;

} Zvalue_value;

struct _zval_struct {

/* Variable Information * *

Zvalue_value value; /* Value * *

Zend_uint RefCount;

Zend_uchar type; /* Active type */

Zend_uchar Is_ref;

};

Test the execution speed of several programs

Test environment:

Os:win7

PHP vesion 5.2.9

apache2.0

1. Determine if the variable value is empty '! ' Add variable name with empty plus variable name do time comparison (cycle 10 million times)

If inside use! $a execution time: 2.40036678314s

If the use of empty ($a) execution time is: 2.52875089645s

Test code:

$start = Microtime (true);

$a = ';

for ($i =0; $i < 10000000; $i + +) {

if (! $a) {//can be changed to empty ($a)

echo "";

}

}

$end = Microtime (true);

echo "Execution Time:", ($end-$start), "s";

?>

2. Use If-esle versus using ternary operators to perform time comparison (10 million cycles)

Use If-else execution time: 2.37516283989s

Use ternary operator to execute time: 2.14390015602s

When the ternary operator runs, each variable is copied once

If-else will not, the impact is very little, just take out and share with you

Test code:

$start = Microtime (true);

$a = range (1, 1000);

for ($i =0; $i < 10000000; $i + +) {

if (Isset ($a)) {

echo "";

}else{

echo "";

}

}

$end = Microtime (true);

echo "Execution Time:", ($end-$start), "s";

?>

  

$start = Microtime (true);

$a = range (1, 1000);

for ($i =0; $i < 10000000; $i + +) {

Isset ($a)? " " : "";

}

$end = Microtime (true);

echo "Execution Time:", ($end-$start), "s";

?>



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.