Comparison between ++ I and I ++ in php. In many programming languages, ++ I and I ++ add + 1 operations to variables, but there are successive problems. let's introduce the differences between them. 1. usage of ++ I (a ++ I and I ++ are used in many programming, and the addition of the Plus 1 operation on the variable is problematic successively, next I will introduce some differences between them.
1. usage of ++ I (using a = ++ I, I = 2 as an example)
Add the I value to 1 (that is, I = I + 1) and then assign it to variable a (that is, a = I ),
The final a value is 3, and the I value is 3.
So a = ++ I is equivalent to I = I + 1, a = I
2. I ++ usage (take a = I ++, I = 2 as an example)
First, assign the I value to variable a (that is, a = I), and then add 1 (that is, I = I + 1) to the I value ),
Then, the final a value is 2, and the I value is 3.
So a = I ++ is equivalent to a = I, I = I + 1
3. ++ I and I ++
A = ++ I is equivalent to I ++, a = I
A = I ++ is equivalent to a = I, I ++
4. when ++ I and I ++ are used independently, they are equivalent to I = I + 1.
If a new variable is assigned, ++ I first adds 1 to the I value, and I ++ first assigns I to the new variable.
Performance Optimization
The code is as follows: |
|
Method 1: $ Begin = time (); $ I = 0; While (++ $ I <10000) { $ J = 0; While (++ $ j <10000) ; ; } $ End = time (); Time: 16 s Method 2: $ Begin = time (); $ I = 0; While ($ I <10000) { $ J = 0; While ($ j <10000) + + $ J; ++ $ I; } $ End = time (); Time: 13 s Method 3: $ Begin = time (); $ I = 0; While ($ I <10000) { $ J = 0; While ($ j <10000) $ J ++; $ I ++; } $ End = time (); Time: 15 s Method 4: $ Begin = time (); $ I = 0; While ($ I ++ <10000) { $ J = 0; While ($ j ++ <10000) ; ; } $ End = time (); Time: 13 s |
Compare the first and second methods, because in PHP, the OPCODE is finally executed, and each line of opline
There are two operands. for an operand, there are generally three types of access methods: temporary variables, variables, and compile-time variables.
Among them, the fastest access is the third type. compiler variables, during OpCode execution, will add a level-1 reference of a variable stored in a hash
Structure, used to speed up access.
Http://www.bkjia.com/PHPjc/629024.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/629024.htmlTechArticle++ I and I ++ are in many programming, the addition of variable + 1 operation, but there are successive problems, below I will introduce them in the running of some of the differences. 1. usage of ++ I (using a = ++...