这个答案是对 Murilo Vasconcelos 的回应:
使用http://gcc.godbolt.org/跟随。
#include <stdio.h>
#include <ctime>
#include <cstdlib>
int main() {
int i;
srand(time(NULL));
for(i=0; i< 10; i++){
printf(" %d ", rand() % 300);
if(i < 10 - 1){
printf("|");
}
}
}
使用 g++-4.8 生成以下程序集:
.LC0:
.string " %d "
main:
pushq %rbp
xorl %edi, %edi
movl $458129845, %ebp
pushq %rbx
xorl %ebx, %ebx
subq $8, %rsp
call time
movl %eax, %edi
call srand
call rand
movl $458129845, %edx
movl $.LC0, %edi
movl %eax, %esi
imull %edx
movl %esi, %eax
sarl $31, %eax
sarl $5, %edx
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
.L2:
movl $124, %edi
addl $1, %ebx
call putchar
call rand
movl $.LC0, %edi
movl %eax, %esi
imull %ebp
movl %esi, %eax
sarl $31, %eax
sarl $5, %edx
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
cmpl $9, %ebx
jne .L2
addq $8, %rsp
xorl %eax, %eax
popq %rbx
popq %rbp
ret
另一方面,这段代码:
#include <stdio.h>
#include <ctime>
#include <cstdlib>
int main() {
int i;
srand(time(NULL));
for (i = 0; i < 9; i++) {
printf(" %d |", rand() % 300);
}
printf(" %d ", rand() % 300);
}
生成此程序集:
.LC0:
.string " %d |"
.LC1:
.string " %d "
main:
pushq %rbx
xorl %edi, %edi
movl $458129845, %ebx
call time
movl %eax, %edi
call srand
call rand
movl $.LC0, %edi
movl %eax, %esi
imull %ebx
movl %esi, %eax
sarl $31, %eax
sarl $5, %edx
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
call rand
movl $.LC0, %edi
movl %eax, %esi
imull %ebx
movl %esi, %eax
sarl $31, %eax
sarl $5, %edx
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
call rand
movl $.LC0, %edi
movl %eax, %esi
imull %ebx
movl %esi, %eax
sarl $31, %eax
sarl $5, %edx
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
call rand
movl $.LC0, %edi
movl %eax, %esi
imull %ebx
movl %esi, %eax
sarl $5, %edx
sarl $31, %eax
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
call rand
movl $.LC0, %edi
movl %eax, %esi
imull %ebx
movl %esi, %eax
sarl $31, %eax
sarl $5, %edx
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
call rand
movl $.LC0, %edi
movl %eax, %esi
imull %ebx
movl %esi, %eax
sarl $31, %eax
sarl $5, %edx
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
call rand
movl $.LC0, %edi
movl %eax, %esi
imull %ebx
movl %esi, %eax
sarl $31, %eax
sarl $5, %edx
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
call rand
movl $.LC0, %edi
movl %eax, %esi
imull %ebx
movl %esi, %eax
sarl $31, %eax
sarl $5, %edx
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
call rand
movl $.LC0, %edi
movl %eax, %esi
imull %ebx
movl %esi, %eax
sarl $31, %eax
sarl $5, %edx
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
call rand
movl $.LC1, %edi
movl %eax, %esi
imull %ebx
movl %esi, %eax
sarl $31, %eax
sarl $5, %edx
subl %eax, %edx
xorl %eax, %eax
imull $300, %edx, %edx
subl %edx, %esi
call printf
xorl %eax, %eax
popq %rbx
ret
换句话说,通过更改循环,它允许编译器展开循环,这应该是一个相当大的性能提升,如果不进行更改,您将无法获得。所以不要让人们对你大便。自己检查一下组装变成了什么,并不是所有的手动优化都是浪费时间。而且,当然,测试测试测试。
但是,当然,您不应该过早地进行优化。你应该关注你的分析器,让它告诉你你的热点是什么,以及你需要优化的地方。
首先是更好的算法。
然后更好的代码。
然后更好的组装。