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. the usage of ++ I (take a ++ I, i2 as an example) first adds the I value to 1 (that is, ii + 1) and then assigns
In many programming languages, ++ I and I ++ add + 1 operations to variables, but there are successive problems. 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 equal to 2, and the I value is equal to 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 instance 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 row of opline has two operands. for the operands, there are generally three types of access methods, temporary variables, variables, and compile-time variables. among these three variables, the fastest access is the third, compiler variables, during OpCode execution, a variable's plus-level reference is stored in a hash
Structure, used to speed up access.