int fact1(int limit)
{
for (i=1;i<limit;i++) fact *= i;
}
int fact2(int limit)
{
for (i=limit; i!=0; i--) fact *= i;
}
这个是当时上学老师出的一道题,我也觉得应该效率一样,不知道各位有什么不同看法
根据10楼兄弟的回答,我用objdump的反汇编结果如下:
00000000004004c2 <fact1>:
4004c2: 55 push %rbp
4004c3: 48 89 e5 mov %rsp,%rbp
4004c6: 89 7d ec mov %edi,-0x14(%rbp)
4004c9: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%rbp)
4004d0: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp)
4004d7: c7 45 f8 01 00 00 00 movl $0x1,-0x8(%rbp)
4004de: eb 0e jmp 4004ee <fact1+0x2c>
4004e0: 8b 45 fc mov -0x4(%rbp),%eax
4004e3: 0f af 45 f8 imul -0x8(%rbp),%eax
4004e7: 89 45 fc mov %eax,-0x4(%rbp)
4004ea: 83 45 f8 01 addl $0x1,-0x8(%rbp)
4004ee: 8b 45 f8 mov -0x8(%rbp),%eax
4004f1: 3b 45 ec cmp -0x14(%rbp),%eax
4004f4: 7c ea jl 4004e0 <fact1+0x1e>
4004f6: 5d pop %rbp
4004f7: c3 retq
00000000004004f8 <fact2>:
4004f8: 55 push %rbp
4004f9: 48 89 e5 mov %rsp,%rbp
4004fc: 89 7d ec mov %edi,-0x14(%rbp)
4004ff: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%rbp)
400506: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp)
40050d: 8b 45 ec mov -0x14(%rbp),%eax
400510: 89 45 f8 mov %eax,-0x8(%rbp)
400513: eb 0e jmp 400523 <fact2+0x2b>
400515: 8b 45 fc mov -0x4(%rbp),%eax
400518: 0f af 45 f8 imul -0x8(%rbp),%eax
40051c: 89 45 fc mov %eax,-0x4(%rbp)
40051f: 83 6d f8 01 subl $0x1,-0x8(%rbp)
400523: 83 7d f8 00 cmpl $0x0,-0x8(%rbp)
400527: 75 ec jne 400515 <fact2+0x1d>
400529: 8b 45 fc mov -0x4(%rbp),%eax
40052c: 5d pop %rbp
40052d: c3 retq
应该和你的觉过一样,减法可以直接判断,加法多了一个mov,但是我这边怎么这么繁琐,没你的简易