关于i++与++i的测试
1.摘要
看到有人说:“i++ 会需要额外保存自增前的值(作为返回值),++i 只是自增变量并将变量自己返回,所以i++会比++i快一点”,那么真的是这样吗?
2. 问题
上网找了找,关于i++和++i两个速度为什么有区别,大致是这么一个说法:
计算机内部实现过程有别,详细如下:
i=i+1的过程相当:
temp=i+1; i=temp;
i++的过程相当:
temp=i; i=temp+1; return temp;
++i的过程最简单:
i增1然后return i 的值,一步完成,没有给任何temp变量赋值
最早是在[http://bbs.chinaunix.net/thread-388165-1-1.html]的1楼发表。
为了验证这个问题我写了一下测试程序,分别以下用4个编译器做测试(我手里只有这几个):
javac 1.6.0_26
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
arm-none-linux-gnueabi-gcc (ctng-1.6.1) 4.4.3
mipsel-linux-gcc (GCC) 4.1.2
3. 测试程序
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class Test{ //测试函数调用 public static void test1(){ int i=0; System.out.println(i++); } public static void test2(){ int i=0; System.out.println(++i); } //测试for循环 public static void test3(){ for(int i=0;i<2;i++); } public static void test4(){ for(int i=0;i<2;++i); } //测试赋值语句 public static void test5(){ int i=0,j=0; j=i++; } public static void test6(){ int i=0,j=0; j=++i; } } |
C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
//测试单独语句 void test1() { int i=0; i++; } void test2() { int i=0; ++i; } //测试for循环 void test3() { int i,j; for(i=0;i<3;i++)j=i; } void test4() { int i,j; for(i=0;i<3;++i)j=i; } //测试赋值语句 void test5() { int i=0,j=0; j=i++; } void test6() { int i=0,j=0; j=++i; } //测试函数调用 void test7(int i) { } void test8() { int i=0; test7(i++); } void test9() { int i=0; test7(++i); } |
javac使用默认选项,gcc使用-g -O0 参数,objdump使用-S参数
4. 反编译结果
java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
Compiled from "Test.java" public class Test extends java.lang.Object{ public Test(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void test1(); Code: 0: iconst_0 1: istore_0 2: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 5: iload_0 6: iinc 0, 1 9: invokevirtual #3; //Method java/io/PrintStream.println:(I)V 12: return public static void test2(); Code: 0: iconst_0 1: istore_0 2: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 5: iinc 0, 1 8: iload_0 9: invokevirtual #3; //Method java/io/PrintStream.println:(I)V 12: return public static void test3(); Code: 0: iconst_0 1: istore_0 2: iload_0 3: iconst_2 4: if_icmpge 13 7: iinc 0, 1 10: goto 2 13: return public static void test4(); Code: 0: iconst_0 1: istore_0 2: iload_0 3: iconst_2 4: if_icmpge 13 7: iinc 0, 1 10: goto 2 13: return public static void test5(); Code: 0: iconst_0 1: istore_0 2: iconst_0 3: istore_1 4: iload_0 5: iinc 0, 1 8: istore_1 9: return public static void test6(); Code: 0: iconst_0 1: istore_0 2: iconst_0 3: istore_1 4: iinc 0, 1 7: iload_0 8: istore_1 9: return } |
gcc:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
void test1() { 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 83 ec 10 sub $0x10,%esp int i=0; 6: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) i++; d: 83 45 fc 01 addl $0x1,-0x4(%ebp) } 11: c9 leave 12: c3 ret 00000013 <test2>: void test2() { 13: 55 push %ebp 14: 89 e5 mov %esp,%ebp 16: 83 ec 10 sub $0x10,%esp int i=0; 19: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) ++i; 20: 83 45 fc 01 addl $0x1,-0x4(%ebp) } 24: c9 leave 25: c3 ret 00000026 <test3>: void test3() { 26: 55 push %ebp 27: 89 e5 mov %esp,%ebp 29: 83 ec 10 sub $0x10,%esp int i,j; for(i=0;i<3;i++)j=i; 2c: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) 33: eb 0a jmp 3f <test3+0x19> 35: 8b 45 fc mov -0x4(%ebp),%eax 38: 89 45 f8 mov %eax,-0x8(%ebp) 3b: 83 45 fc 01 addl $0x1,-0x4(%ebp) 3f: 83 7d fc 02 cmpl $0x2,-0x4(%ebp) 43: 7e f0 jle 35 <test3+0xf> } 45: c9 leave 46: c3 ret 00000047 <test4>: void test4() { 47: 55 push %ebp 48: 89 e5 mov %esp,%ebp 4a: 83 ec 10 sub $0x10,%esp int i,j; for(i=0;i<3;++i)j=i; 4d: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) 54: eb 0a jmp 60 <test4+0x19> 56: 8b 45 fc mov -0x4(%ebp),%eax 59: 89 45 f8 mov %eax,-0x8(%ebp) 5c: 83 45 fc 01 addl $0x1,-0x4(%ebp) 60: 83 7d fc 02 cmpl $0x2,-0x4(%ebp) 64: 7e f0 jle 56 <test4+0xf> } 66: c9 leave 67: c3 ret 00000068 <test5>: void test5() { 68: 55 push %ebp 69: 89 e5 mov %esp,%ebp 6b: 83 ec 10 sub $0x10,%esp int i=0,j=0; 6e: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) 75: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%ebp) j=i++; 7c: 8b 45 fc mov -0x4(%ebp),%eax 7f: 89 45 f8 mov %eax,-0x8(%ebp) 82: 83 45 fc 01 addl $0x1,-0x4(%ebp) } 86: c9 leave 87: c3 ret 00000088 <test6>: void test6() { 88: 55 push %ebp 89: 89 e5 mov %esp,%ebp 8b: 83 ec 10 sub $0x10,%esp int i=0,j=0; 8e: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) 95: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%ebp) j=++i; 9c: 83 45 fc 01 addl $0x1,-0x4(%ebp) a0: 8b 45 fc mov -0x4(%ebp),%eax a3: 89 45 f8 mov %eax,-0x8(%ebp) } a6: c9 leave a7: c3 ret 000000a8 <test7>: void test7(int i) { a8: 55 push %ebp a9: 89 e5 mov %esp,%ebp } ab: 5d pop %ebp ac: c3 ret 000000ad <test8>: void test8() { ad: 55 push %ebp ae: 89 e5 mov %esp,%ebp b0: 83 ec 14 sub $0x14,%esp int i=0; b3: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) test7(i++); ba: 8b 45 fc mov -0x4(%ebp),%eax bd: 83 45 fc 01 addl $0x1,-0x4(%ebp) c1: 89 04 24 mov %eax,(%esp) c4: e8 fc ff ff ff call c5 <test8+0x18> } c9: c9 leave ca: c3 ret 000000cb <test9>: void test9() { cb: 55 push %ebp cc: 89 e5 mov %esp,%ebp ce: 83 ec 14 sub $0x14,%esp int i=0; d1: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) test7(++i); d8: 83 45 fc 01 addl $0x1,-0x4(%ebp) dc: 8b 45 fc mov -0x4(%ebp),%eax df: 89 04 24 mov %eax,(%esp) e2: e8 fc ff ff ff call e3 <test9+0x18> } e7: c9 leave e8: c3 ret |
arm-none-linux-gnueabi-gcc (ctng-1.6.1) 4.4.3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
00000000 <test1>: void test1() { 0: e52db004 push {fp} ; (str fp, [sp, #-4]!) 4: e28db000 add fp, sp, #0 8: e24dd00c sub sp, sp, #12 int i=0; c: e3a03000 mov r3, #0 10: e50b3008 str r3, [fp, #-8] i++; 14: e51b3008 ldr r3, [fp, #-8] 18: e2833001 add r3, r3, #1 1c: e50b3008 str r3, [fp, #-8] } 20: e28bd000 add sp, fp, #0 24: e8bd0800 pop {fp} 28: e12fff1e bx lr 0000002c <test2>: void test2() { 2c: e52db004 push {fp} ; (str fp, [sp, #-4]!) 30: e28db000 add fp, sp, #0 34: e24dd00c sub sp, sp, #12 int i=0; 38: e3a03000 mov r3, #0 3c: e50b3008 str r3, [fp, #-8] ++i; 40: e51b3008 ldr r3, [fp, #-8] 44: e2833001 add r3, r3, #1 48: e50b3008 str r3, [fp, #-8] } 4c: e28bd000 add sp, fp, #0 50: e8bd0800 pop {fp} 54: e12fff1e bx lr 00000058 <test3>: void test3() { 58: e52db004 push {fp} ; (str fp, [sp, #-4]!) 5c: e28db000 add fp, sp, #0 60: e24dd00c sub sp, sp, #12 int i,j; for(i=0;i<3;i++)j=i; 64: e3a03000 mov r3, #0 68: e50b300c str r3, [fp, #-12] 6c: ea000004 b 84 <test3+0x2c> 70: e51b300c ldr r3, [fp, #-12] 74: e50b3008 str r3, [fp, #-8] 78: e51b300c ldr r3, [fp, #-12] 7c: e2833001 add r3, r3, #1 80: e50b300c str r3, [fp, #-12] 84: e51b300c ldr r3, [fp, #-12] 88: e3530002 cmp r3, #2 8c: dafffff7 ble 70 <test3+0x18> } 90: e28bd000 add sp, fp, #0 94: e8bd0800 pop {fp} 98: e12fff1e bx lr 0000009c <test4>: void test4() { 9c: e52db004 push {fp} ; (str fp, [sp, #-4]!) a0: e28db000 add fp, sp, #0 a4: e24dd00c sub sp, sp, #12 int i,j; for(i=0;i<3;++i)j=i; a8: e3a03000 mov r3, #0 ac: e50b300c str r3, [fp, #-12] b0: ea000004 b c8 <test4+0x2c> b4: e51b300c ldr r3, [fp, #-12] b8: e50b3008 str r3, [fp, #-8] bc: e51b300c ldr r3, [fp, #-12] c0: e2833001 add r3, r3, #1 c4: e50b300c str r3, [fp, #-12] c8: e51b300c ldr r3, [fp, #-12] cc: e3530002 cmp r3, #2 d0: dafffff7 ble b4 <test4+0x18> } d4: e28bd000 add sp, fp, #0 d8: e8bd0800 pop {fp} dc: e12fff1e bx lr 000000e0 <test5>: void test5() { e0: e52db004 push {fp} ; (str fp, [sp, #-4]!) e4: e28db000 add fp, sp, #0 e8: e24dd00c sub sp, sp, #12 int i=0,j=0; ec: e3a03000 mov r3, #0 f0: e50b300c str r3, [fp, #-12] f4: e3a03000 mov r3, #0 f8: e50b3008 str r3, [fp, #-8] j=i++; fc: e51b300c ldr r3, [fp, #-12] 100: e50b3008 str r3, [fp, #-8] 104: e51b300c ldr r3, [fp, #-12] 108: e2833001 add r3, r3, #1 10c: e50b300c str r3, [fp, #-12] } 110: e28bd000 add sp, fp, #0 114: e8bd0800 pop {fp} 118: e12fff1e bx lr 0000011c <test6>: void test6() { 11c: e52db004 push {fp} ; (str fp, [sp, #-4]!) 120: e28db000 add fp, sp, #0 124: e24dd00c sub sp, sp, #12 int i=0,j=0; 128: e3a03000 mov r3, #0 12c: e50b300c str r3, [fp, #-12] 130: e3a03000 mov r3, #0 134: e50b3008 str r3, [fp, #-8] j=++i; 138: e51b300c ldr r3, [fp, #-12] 13c: e2833001 add r3, r3, #1 140: e50b300c str r3, [fp, #-12] 144: e51b300c ldr r3, [fp, #-12] 148: e50b3008 str r3, [fp, #-8] } 14c: e28bd000 add sp, fp, #0 150: e8bd0800 pop {fp} 154: e12fff1e bx lr 00000158 <test7>: void test7(int i) { 158: e52db004 push {fp} ; (str fp, [sp, #-4]!) 15c: e28db000 add fp, sp, #0 160: e24dd00c sub sp, sp, #12 164: e50b0008 str r0, [fp, #-8] } 168: e28bd000 add sp, fp, #0 16c: e8bd0800 pop {fp} 170: e12fff1e bx lr 00000174 <test8>: void test8() { 174: e92d4800 push {fp, lr} 178: e28db004 add fp, sp, #4 17c: e24dd008 sub sp, sp, #8 int i=0; 180: e3a03000 mov r3, #0 184: e50b3008 str r3, [fp, #-8] test7(i++); 188: e51b3008 ldr r3, [fp, #-8] 18c: e51b2008 ldr r2, [fp, #-8] 190: e2822001 add r2, r2, #1 194: e50b2008 str r2, [fp, #-8] 198: e1a00003 mov r0, r3 19c: ebfffffe bl 158 <test7> } 1a0: e24bd004 sub sp, fp, #4 1a4: e8bd4800 pop {fp, lr} 1a8: e12fff1e bx lr 000001ac <test9>: void test9() { 1ac: e92d4800 push {fp, lr} 1b0: e28db004 add fp, sp, #4 1b4: e24dd008 sub sp, sp, #8 int i=0; 1b8: e3a03000 mov r3, #0 1bc: e50b3008 str r3, [fp, #-8] test7(++i); 1c0: e51b3008 ldr r3, [fp, #-8] 1c4: e2833001 add r3, r3, #1 1c8: e50b3008 str r3, [fp, #-8] 1cc: e51b0008 ldr r0, [fp, #-8] 1d0: ebfffffe bl 158 <test7> } 1d4: e24bd004 sub sp, fp, #4 1d8: e8bd4800 pop {fp, lr} 1dc: e12fff1e bx lr |
mipsel-linux-gcc (GCC) 4.1.2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
00000000 <test1>: void test1() { 0: 27bdffe8 addiu sp,sp,-24 4: afbe0010 sw s8,16(sp) 8: 03a0f021 move s8,sp int i=0; c: afc00008 sw zero,8(s8) i++; 10: 8fc20008 lw v0,8(s8) 14: 00000000 nop 18: 24420001 addiu v0,v0,1 1c: afc20008 sw v0,8(s8) } 20: 03c0e821 move sp,s8 24: 8fbe0010 lw s8,16(sp) 28: 27bd0018 addiu sp,sp,24 2c: 03e00008 jr ra 30: 00000000 nop 00000034 <test2>: void test2() { 34: 27bdffe8 addiu sp,sp,-24 38: afbe0010 sw s8,16(sp) 3c: 03a0f021 move s8,sp int i=0; 40: afc00008 sw zero,8(s8) ++i; 44: 8fc20008 lw v0,8(s8) 48: 00000000 nop 4c: 24420001 addiu v0,v0,1 50: afc20008 sw v0,8(s8) } 54: 03c0e821 move sp,s8 58: 8fbe0010 lw s8,16(sp) 5c: 27bd0018 addiu sp,sp,24 60: 03e00008 jr ra 64: 00000000 nop 00000068 <test3>: void test3() { 68: 27bdffe8 addiu sp,sp,-24 6c: afbe0010 sw s8,16(sp) 70: 03a0f021 move s8,sp int i,j; for(i=0;i<3;i++)j=i; 74: afc0000c sw zero,12(s8) 78: 10000008 b 9c <test3+0x34> 7c: 00000000 nop 80: 8fc2000c lw v0,12(s8) 84: 00000000 nop 88: afc20008 sw v0,8(s8) 8c: 8fc2000c lw v0,12(s8) 90: 00000000 nop 94: 24420001 addiu v0,v0,1 98: afc2000c sw v0,12(s8) 9c: 8fc2000c lw v0,12(s8) a0: 00000000 nop a4: 28420003 slti v0,v0,3 a8: 1440fff5 bnez v0,80 <test3+0x18> ac: 00000000 nop } b0: 03c0e821 move sp,s8 b4: 8fbe0010 lw s8,16(sp) b8: 27bd0018 addiu sp,sp,24 bc: 03e00008 jr ra c0: 00000000 nop 000000c4 <test4>: void test4() { c4: 27bdffe8 addiu sp,sp,-24 c8: afbe0010 sw s8,16(sp) cc: 03a0f021 move s8,sp int i,j; for(i=0;i<3;++i)j=i; d0: afc0000c sw zero,12(s8) d4: 10000008 b f8 <test4+0x34> d8: 00000000 nop dc: 8fc2000c lw v0,12(s8) e0: 00000000 nop e4: afc20008 sw v0,8(s8) e8: 8fc2000c lw v0,12(s8) ec: 00000000 nop f0: 24420001 addiu v0,v0,1 f4: afc2000c sw v0,12(s8) f8: 8fc2000c lw v0,12(s8) fc: 00000000 nop 100: 28420003 slti v0,v0,3 104: 1440fff5 bnez v0,dc <test4+0x18> 108: 00000000 nop } 10c: 03c0e821 move sp,s8 110: 8fbe0010 lw s8,16(sp) 114: 27bd0018 addiu sp,sp,24 118: 03e00008 jr ra 11c: 00000000 nop 00000120 <test5>: void test5() { 120: 27bdffe8 addiu sp,sp,-24 124: afbe0010 sw s8,16(sp) 128: 03a0f021 move s8,sp int i=0,j=0; 12c: afc0000c sw zero,12(s8) 130: afc00008 sw zero,8(s8) j=i++; 134: 8fc2000c lw v0,12(s8) 138: 00000000 nop 13c: afc20008 sw v0,8(s8) 140: 8fc2000c lw v0,12(s8) 144: 00000000 nop 148: 24420001 addiu v0,v0,1 14c: afc2000c sw v0,12(s8) } 150: 03c0e821 move sp,s8 154: 8fbe0010 lw s8,16(sp) 158: 27bd0018 addiu sp,sp,24 15c: 03e00008 jr ra 160: 00000000 nop 00000164 <test6>: void test6() { 164: 27bdffe8 addiu sp,sp,-24 168: afbe0010 sw s8,16(sp) 16c: 03a0f021 move s8,sp int i=0,j=0; 170: afc0000c sw zero,12(s8) 174: afc00008 sw zero,8(s8) j=++i; 178: 8fc2000c lw v0,12(s8) 17c: 00000000 nop 180: 24420001 addiu v0,v0,1 184: afc2000c sw v0,12(s8) 188: 8fc2000c lw v0,12(s8) 18c: 00000000 nop 190: afc20008 sw v0,8(s8) } 194: 03c0e821 move sp,s8 198: 8fbe0010 lw s8,16(sp) 19c: 27bd0018 addiu sp,sp,24 1a0: 03e00008 jr ra 1a4: 00000000 nop 000001a8 <test7>: void test7(int i) { 1a8: 27bdfff8 addiu sp,sp,-8 1ac: afbe0000 sw s8,0(sp) 1b0: 03a0f021 move s8,sp 1b4: afc40008 sw a0,8(s8) } 1b8: 03c0e821 move sp,s8 1bc: 8fbe0000 lw s8,0(sp) 1c0: 27bd0008 addiu sp,sp,8 1c4: 03e00008 jr ra 1c8: 00000000 nop 000001cc <test8>: void test8() { 1cc: 3c1c0000 lui gp,0x0 1d0: 279c0000 addiu gp,gp,0 1d4: 0399e021 addu gp,gp,t9 1d8: 27bdffd8 addiu sp,sp,-40 1dc: afbf0024 sw ra,36(sp) 1e0: afbe0020 sw s8,32(sp) 1e4: 03a0f021 move s8,sp 1e8: afbc0010 sw gp,16(sp) int i=0; 1ec: afc00018 sw zero,24(s8) test7(i++); 1f0: 8fc30018 lw v1,24(s8) 1f4: 8fc20018 lw v0,24(s8) 1f8: 00000000 nop 1fc: 24420001 addiu v0,v0,1 200: afc20018 sw v0,24(s8) 204: 00602021 move a0,v1 208: 8f990000 lw t9,0(gp) 20c: 00000000 nop 210: 0320f809 jalr t9 214: 00000000 nop 218: 8fdc0010 lw gp,16(s8) } 21c: 03c0e821 move sp,s8 220: 8fbf0024 lw ra,36(sp) 224: 8fbe0020 lw s8,32(sp) 228: 27bd0028 addiu sp,sp,40 22c: 03e00008 jr ra 230: 00000000 nop 00000234 <test9>: void test9() { 234: 3c1c0000 lui gp,0x0 238: 279c0000 addiu gp,gp,0 23c: 0399e021 addu gp,gp,t9 240: 27bdffd8 addiu sp,sp,-40 244: afbf0024 sw ra,36(sp) 248: afbe0020 sw s8,32(sp) 24c: 03a0f021 move s8,sp 250: afbc0010 sw gp,16(sp) int i=0; 254: afc00018 sw zero,24(s8) test7(++i); 258: 8fc20018 lw v0,24(s8) 25c: 00000000 nop 260: 24420001 addiu v0,v0,1 264: afc20018 sw v0,24(s8) 268: 8fc40018 lw a0,24(s8) 26c: 8f990000 lw t9,0(gp) 270: 00000000 nop 274: 0320f809 jalr t9 278: 00000000 nop 27c: 8fdc0010 lw gp,16(s8) } 280: 03c0e821 move sp,s8 284: 8fbf0024 lw ra,36(sp) 288: 8fbe0020 lw s8,32(sp) 28c: 27bd0028 addiu sp,sp,40 290: 03e00008 jr ra 294: 00000000 nop |
5. 结论
java:
1、for循环中的i++和++i完全没有区别。
2、需要赋值的情况下字节码指令相同,顺序不同。
gcc:
1、for循环中的i++和++i完全没区别。
2、需要赋值的情况下机器指令完全相同,顺序不同。
arm-linux-gcc:
1、for循环中的i++和++i完全没区别
2、赋值语句的机器指令完全相同,顺序不同
3、调用语句中,i++比++i多一条mov r0, r3 指令,其余指令相同,使用寄存器不同,指令顺序不同。
mipsel-linux-gcc:
1、for循环中的i++和++i完全没区别
2、赋值语句的机器指令完全相同,顺序不同
3、调用语句中,i++比++i多一条move a0,v1 指令,其余指令相同,使用寄存器不同,指令顺序不同。
以上结论针对的是-O0,没有测试O2下的区别。
还看到这么一篇文章:
http://blog.csdn.net/zy1691/article/details/4849808
其中有这么一段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
i++情况 ; 9 : int c; ; 10 : for(int i=0;i<=50;i++) mov DWORD PTR _i$20035[ebp], 0 jmp SHORT $LN3@wmain $LN2@wmain: mov eax, DWORD PTR _i$20035[ebp] add eax, 1 mov DWORD PTR _i$20035[ebp], eax $LN3@wmain: cmp DWORD PTR _i$20035[ebp], 50 ; 00000032H jg SHORT $LN1@wmain ; 11 : c=c+i; cmp BYTE PTR $T25811[ebp], 0 jne SHORT $LN6@wmain push OFFSET $LN7@wmain call __RTC_UninitUse add esp, 4 ++i情况 ; 9 : int c; ; 10 : for(int i=0;i<=50;++i) mov DWORD PTR _i$20035[ebp], 0 jmp SHORT $LN3@wmain $LN2@wmain: mov eax, DWORD PTR _i$20035[ebp] add eax, 1 mov DWORD PTR _i$20035[ebp], eax $LN3@wmain: cmp DWORD PTR _i$20035[ebp], 50 ; 00000032H jg SHORT $LN1@wmain ; 11 : c=c+i; cmp BYTE PTR $T25811[ebp], 0 jne SHORT $LN6@wmain push OFFSET $LN7@wmain call __RTC_UninitUse add esp, 4 $LN6@wmain: mov eax, DWORD PTR _c$[ebp] add eax, DWORD PTR _i$20035[ebp] mov BYTE PTR $T25811[ebp], 1 mov DWORD PTR _c$[ebp], eax jmp SHORT $LN2@wmain $LN1@wmain: 可以看出++i确实省去了对内存操作的环节,直接add eax, 1,用++i真的能提高性能。 当然,上面是用debug版本,也就是说没有优化,那么在release优化的情况下会怎样呢。 继续试验 |
但是我看了半天也没看出两段代码究竟有什么区别。。。
本作品采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可,转载请注明作者及原网址。
抱歉,暂停评论。