INSIDE I ++

zhaozj2021-02-08  289

i , i, i = i 1, how is efficiency? I have seen a book saying that i is better than i = i 1 because I = i 1 1 to take up a register, so speed is not i fast, so I want to verify. In addition, I have heard that "i = i " in Java does not have the correct conclusion, that is, "first accumulate and re-value", but Java has passed this kind of operation, I have no changes in the I value. So here, think about it and verify these questions in C.

===================== Test the C source program ===============================================================================================================================================================================================

# 01: #include # 02: # 03: main () # 04: {# 05: I = 0, j = 0; # 06: i = i ; # 07: # 08: i = i 1; # 09: i ; # 10: i; # 11: # 12: j = i 1; # 13: j = i ; # 14: j = i; # 15: # 16: Printf ("i =% d", i); # 17:}

============================================================================================================================================================================================================= ====

Here is the anti-assessment of the above source program in the VC 6.0 SP5 / Window 2000 environment:

5: INT i = 0, J = 0; 0040D698 MOV DWORD PTR [EBP-4], 00040D69F MOV DWORD PTR [EBP-8], 0

6: i = i ; 0040D6A6 MOV EAX, DWORD PTR [EBP-4] 0040D6A9 MOV DWORD PTR [EBP-4], EAX0040D6AC MOV ECX, DWORD PTR [EBP-4] 0040D6AF ADD ECX, 10040D6B2 MOV DWORD PTR [EBP-4 ], ECX

8: i = i 1; 0040D6B5 MOV EDX, DWORD PTR [EBP-4] 0040D6B8 Add EDX, 10040D6BB MOV DWORD PTR [EBP-4], EDX

9: i ; 0040D6BE MOV EAX, DWORD PTR [EBP-4] 0040D6C1 Add Eax, 10040D6C4 MOV DWORD PTR [EBP-4], EAX10: i; 0040D6C7 MOV ECX, DWORD PTR [EBP-4] 0040D6CA Add ECX, 10040D6CD MOV DWORD PTR [EBP-4], ECX

12: J = i 1; 0040D6D0 MOV EDX, DWORD PTR [EBP-4] 0040D6D3 Add EDX, 10040D6D6 MOV DWORD PTR [EBP-8], EDX

13: J = i ; 0040D6D9 MOV EAX, DWORD PTR [EBP-4] 0040D6DC MOV DWORD PTR [EBP-8], EAX0040D6DF MOV ECX, DWORD PTR [EBP-4] 0040D6E2 Add ECX, 10040D6E5 MOV DWORD PTR [EBP-4 ], ECX

14: J = i; 0040D6E8 MOV EDX, DWORD PTR [EBP-4] 0040D6EB Add EDX, 10040D6EE MOV DWORD PTR [EBP-4], EDX0040D6F1 MOV EAX, DWORD PTR [EBP-4] 0040D6F4 MOV DWORD PTR [EBP -8], EAX

============================================================================================================================================================================================================= ================

Below is the memory assembly code that I use to use DBX after the SCO UNIX is compiled by CC -G, using DBX's memory assembly code:

(INT i = 0, J = 0;) 0x0000015E (Main: 5) MOV DWORD PTR [EBP-0x04], $ 00X00000165 (Main: 5) MOV DWORD PTR [EBP-0x08], $ 0

(i = i ;) 0x0000016C (Main: 6) MOV EAX, DWORD PTR [EBP-0x04] 0x0000016F (Main: 6) Inc DWORD PTR [EBP-0x04] 0x00000172 (Main: 6) MOV DWORD PTR [EBP-0x04] EAX

(i = i 1;) 0x00000175 (Main: 8) Inc DWORD PTR [EBP-0x04]

(i ;) 0x00000178 (Main: 9) Inc DWORD PTR [EBP-0X04] ( i;) 0x0000017B (Main: 10) Inc DWORD PTR [EBP-0x04]

(j = i 1;) 0x0000017E (Main: 12) MOV Eax, DWORD PTR [EBP-0x04] 0x00000181 (Main: 12) Inc EAX0X00000182 (Main: 12) MOV DWORD PTR [EBP-0x08], EBP-0X08], EBP-0X08]

(j = i ;) 0x00000185 (Main: 13) MOV EAX, DWORD PTR [EBP-0x04] 0x00000188 (Main: 13) Inc DWORD PTR [EBP-0X04] 0x0000018B (Main: 13) MOV DWORD PTR [EBP-0x08] EAX

(J = i;) 0x0000018E (Main: 14) Inc DWORD PTR [EBP-0x04] 0x00000191 (Main: 14) MOV EAX, DWORD PTR [EBP-0x04] 0x00000194 (Main: 14) MOV DWORD PTR [EBP- 0x08], EAX

============================================================================================================================================================================================================= ====================

1. From the above assembly code, we are not difficult to see for i = i 1; i ; i these three instructions are the same under VC or on the CC of SCO UNIX (although these two The compiler is not only the same, but they are the same as the assembly of these three instructions. Here, I am closing the compiler optimization option, maybe the current compiler is automatically optimized)

At the VC, it is: 0040D6B5 MOV EDX, DWORD PTR [EBP-4] 0040D6B8 Add EDX, 1 0040D6BM MOV DWORD PTR [EBP-4], EDX

At CC, it is: 0x0000017B (Main: 10) Inc DWORD PTR [EBP-0x04]

2, for the composite command J = i 1; j = i ; there is different, under VC: j = i 1 is three instructions, and J = i has five instructions, which is also very reasonable. Under SCO: j = i 1 and j = i are all three instructions. (J = i instruction number ratio Vc less) 3, for i = i , the correct result i = 1 can be obtained under VC; but it is I = 0 in SCO; this can be seen from its compilation.

The container of VC on i = i is:

0040D6A6 MOV EAX, DWORD PTR [EBP-4] // Take the memory value I to EAX 0040D6A9 MOV DWORD PTR [EBP-4], EAX // put the EAX value in memory I 0040D6AC MOV ECX, DWORD PTR [EBP-4 ] // Take the memory value I to ECX 0040D6AF ADD ECX, 1 // Register ECX plus 1 0040d6b2 MOV DWORD PTR [EBP-4], ECX // put the ECX value into memory i

The assembly of SCO to i = i is: // Take memory I to register EAX 0x0000016C (Main: 6) MOV Eax, DWORD PTR [EBP-0x04]

// Tired of memory i 0x0000016F (Main: 6) Inc DWORD PTR [EBP-0X04] // put the register EAX value 0x00000172 (Main: 6) MOV DWORD PTR [EBP-0x04], EBP-0x04], EBP-0x04]

It can be seen that the reason why SCO does not get the right result. I have a comment for it, I believe that you can understand.

[Conclusion]: 1, if it is a single voice, whether it is i = i 1; i ; i; its efficiency is exactly the same. 2, the reason why I = i under SCO does not have the correct result because its compiler pursues efficiency results. (Java is also said) 3. Observe the assembly instructions under the SCO "Inc DWORD PTR [EBP-0x04]", can you directly operate memory? (Not allowed) 4, each manufacturer has different compilers, and the instruction code of the statement generated is different, and C is more likely.

============================================================================================================================================================================================================= ====================

Finally, Java is tested to i = i

Source program:

# 1: Class test # 2: {# 3: public static void main (string [] argv) # 4: {# 5: I = 0; # 6: i = i ; # 7: system.out.println "i =" i); # 8:} # 9:} Compiled it into test.class with Javac -g Test.java. Use Javap -C Test to play its virtual machine instruction code as follows:

D: /> javap -c test

Compiled from test.javaclass test extends java.lang.object {test (); public static void main (java.lang.string ";}

Method test () 0 aload_0 1 invokespecial # 1 4 Return

Method void main (java.lang.string "[]) 0 iconst_0 1 iStore_1 2 iLoad_1 3 IINC 1 1 6 iStore_1 7 getStatic # 2 10 New # 3 13 DUP 14 InvokeSpecial # 4 17 LDC # 5 转载请注明原文地址:https://www.9cbs.com/read-1144.html


New Post(0)