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 (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: |
Copy code |
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.