++i and i++ are available in many programs, adding +1 to the variable, but there are problems, let me introduce some of the differences they are running.
1, the use of ++i (take A=++i, i=2 as an example)
The I value is first added to 1 (that is, i=i+1) and then assigned to the variable a (that is, a=i),
Then the final a value is equal to 3, and the I value equals 3.
So a=++i equivalent to i=i+1, a=i
2, the use of i++ (take a=i++, i=2 as an example)
First assign the I value to the variable a (that is, a=i), then I value 1 (that is, i=i+1),
Then the final a value is equal to 2, and the I value equals 3.
So a=i++ equivalent to A=i, i=i+1
3, ++i and i++
A=++i equivalent to i++, a=i
a=i++ equivalent to A=i, i++
4, ++i and i++ when used alone, equivalent to I=i+1
If you assign a new variable, the ++i first assigns the I value to 1 and the i++ first to the new variable.
Performance optimization
The code is as follows |
Copy Code |
Way One:
$begin = time (); $i = 0; while (+ + $i < 10000) { $j = 0; while (+ + $j < 10000) ; ; } $end = time ();
Time: 16s
Way two:
$begin = time (); $i = 0; while ($i < 10000) { $j = 0; while ($j < 10000) + + $j; + + $i; } $end = time ();
Time: 13s
Way three:
$begin = time (); $i = 0; while ($i < 10000) { $j = 0; while ($j < 10000) $j + +; $i + +; } $end = time ();
Time: 15s
Mode four:
$begin = time (); $i = 0; while ($i + < 10000) { $j = 0; while ($j + < 10000) ; ; } $end = time (); Time: 13s |
Compare the first method and the second method, because in PHP, the final execution is opcode, each line opline
There are two operands, and for operands there are generally 3 types of access, temporary variables, variables, and compile-time variables, three variables
Among them, the fastest access is the third, the compiler variable, in the opcode execution process, will say a variable plus a reference stored in a hash
Structure, which is used to speed up access.
http://www.bkjia.com/PHPjc/629024.html www.bkjia.com true http://www.bkjia.com/PHPjc/629024.html techarticle ++i and i++ are available in many programs, adding +1 to the variable, but there are problems, let me introduce some of the differences they are running. 1, the use of ++i (a=++ ...