I will share my PHP Research with you.

Source: Internet
Author: User
A little insight (refer to the snow and the relevant information on the Internet): the difference between reference and replication: We all know that the reference is actually: $ a = 1; $ B = $; changing $ a or $ B does not affect the other party. The reference is to pass the value by address: $ a = 1; $ B = & amp; $ a; change any other value... "> <LINKhref =" http://www.php100.com//st

Little insights (refer to the snow and snow corners and relevant online materials ):

Differences between reference and replication:

We all know that the reference is: $ a = 1; $ B = $ a; changing $ a or $ B will not affect the other party. The reference is to pass the value by address: $ a = 1; $ B = & $ a; changing any other value will change.

But add a why at the end. why?

You need to understand the two mechanisms in php: copy on write and change on write:

Copy on write: copy when writing; apply to variable copy. For example, in the preceding example, the $ a = 1; $ a variable is stored in the symbol table, and the value "1" is stored in zval. That is to say, it is stored in php.

Each variable name is stored in the symbol table, and each symbol corresponds to a zval; zval can be seen as a container with variable values, variable types, and so on.

$ A = 1; $ B = $ a; someone may think that there are two "1" in the memory, but this is not actually the case, both $ a and $ B point to the zval container that stores 1. It can be illustrated that $ a and $ B each have a line pointing

Zval and zval have a refcount parameter, which is used to count the number of lines pointing to itself. When there is a variable for replication, refcount will add 1. That is, $ a = 1; $ B = $;

The refcount value in the zval structure of 1 is 2. There is a function in php that can be viewed, that is, debug_zval_dump ($ v); when debug_zval_dump ($ a); the count is 3, because $ a is

The printed value is 3 because it is passed to debug_zval_dump.

Next we will illustrate the problem: unset, which is used to reduce the number of refcount of the variable by one. That is, when the refcount value is 0, the memory occupied by the variable value will be released. When unset ($ B)

1. Instead of releasing it in the memory, it points to its reference subtraction.

The memory release mentioned in may not be released to the system (OS). php will open up a specified memory size for itself. When a variable is generated, it will apply in its own memory zone, it is released to

Memory area. It is very helpful for improving efficiency. However, when the value exceeds the specified value, the system will apply for memory, and then the system will release the memory.

For references, there is another parameter is_ref in zval to Mark (Mark whether it is a real reference). The default value is 0, for example, $ a = 1; $ B = & $; in this case, debug_zval_dump ($ a); the result is 1,

Refcount = 2, is_ref = 3,

The zval consortium in source code 5.2.17 can be downloaded from the official website.

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;

};

Testing the execution speed of several programs

Test environment:

OS: win7

Php vesion 5.2.9

Apache2.0

1. Determine whether the variable value is null '! 'Compare the variable name with the empty variable name for execution time (10 million cycles)

If! $ A Execution time: 2.40036678314 s

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

Test code:

$ Start = microtime (true );

$ A = '';

For ($ I = 0; I I <10000000; $ I ++ ){

If (! $ A) {// you can change it to empty ($)

Echo "";

}

}

$ End = microtime (true );

Echo "Execution time:", ($ end-$ start), "s ";

?>

 

2. Use if-esle to compare the execution time with the use of the ternary operator (loop 10 million times)

If-else Execution time: 2.37516283989 s

The execution time of the ternary operator is 2.14390015602 s.

When the ternary operator is running, each variable is copied once.

If-else does not, but it has little impact. I just want to share it with you.

Test code:

$ Start = microtime (true );

$ A = range (1, 1000 );

For ($ I = 0; I I <10000000; $ I ++ ){

If (isset ($ )){

Echo "";

} Else {

Echo "";

}

}

$ End = microtime (true );

Echo "Execution time:", ($ end-$ start), "s ";

?>

$ Start = microtime (true );

$ A = range (1, 1000 );

For ($ I = 0; I I <10000000; $ I ++ ){

Isset ($ )? "":"";

}

$ 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.