I used to think that xx1, x1, and x are only different in writing representation methods. I didn't expect to study them carefully ....... From the process above, we can see that x is less of a write process than x1, while x1 is less of an addressing process than xx1 (finding the address of x on the left ), therefore, the efficiency of the three statements is xx1x1x.
I used to think that x = x + 1, x + = 1, and x ++ are only different in writing representation methods. I did not expect to study it carefully, but the difference is not small.
X = x + 1 is the lowest because the execution process is as follows:
- Read the memory address of the right variable x;
- Execute x + 1;
- Read the memory address of variable x on the left;
- Pass the value on the right to the variable x on the left (the compiler does not think that the memory address of the variable x on both sides is the same ).
First Look for the address on the right side of x, then read the value of x in the address, and then add the value of x to 1 in the register, next, find the address of x on the left (the computer does not know that x on the left is x on the right), and then save the calculation result to the address of x on the left.
X = + 1. the execution process is as follows:
- Read the memory address of the right variable x;
- Run x = 1;
- Pass the obtained value to variable x (because the memory address of variable x has been read ).
Find the address of x, read the value of x in the address, and add the value of x to 1 in the register, then save the calculation result to the address of x (here the computer knows that the address for reading and writing operations is the same ).
X ++ is the highest, and its execution is as follows:
- Read the memory address of the right variable x;
- Variable x auto-Increment 1.
Find the address of x, read the value of x in the address, and then add the value of 1 to the address.
From the process above, we can see that x ++ is less than x + = 1, x + = 1 is less addressing than x = x + 1 (search for the address of x on the left ), therefore, the efficiency of the three statements is x = x + 1 <x + = 1 <x ++.
This article is available at http://www.nowamagic.net/librarys/veda/detail/616.