循环展开
2019-07-13 19:29发布
生成海报
循环展开:减少循环次数来提高程序性能。如果展开次数太多,反而会造成性能急剧下降。因为展开次数太多,那么运算过程中中间变量会很多,而计算机的寄存器个数是固定的,当变量个数超了寄存
器,那么变量只能存到栈中从而导致性能下降
实例代码如下
long long getSystemTime() {
struct timeb t;
ftime(&t);
return 1000 * t.time + t.millitm;
}
int main()
{
int * b_test_data = (int *)malloc(80000000*sizeof(int));
int b_loop;
long long b_begin, b_end, b_ttl = 0;
for(b_loop = 0; b_loop < 80000000; b_loop++)
{
b_test_data[b_loop] = 1;
}
b_begin = getSystemTime();
for(b_loop = 0; b_loop < 80000000; b_loop++)
{
b_ttl = b_ttl + b_test_data[b_loop];
}
b_end = getSystemTime();
printf("time = %lu, b_ttl = %lu
", b_end - b_begin, b_ttl);
return 0;
}
执行结果:
展开:
int main()
{
int * b_test_data = (int *)malloc(80000000*sizeof(int));
int b_loop;
long long b_begin, b_end, b_ttl = 0;
for(b_loop = 0; b_loop < 80000000; b_loop++)
{
b_test_data[b_loop] = 1;
}
b_begin = getSystemTime();
/*循环展开操作*/
for(b_loop = 0; b_loop < 80000000 - 1; b_loop+=2)
{
b_ttl = b_ttl + b_test_data[b_loop] + b_test_data[b_loop + 1];
}
for(; b_loop < 80000000; b_loop++)
{
b_ttl = b_ttl + b_test_data[b_loop];
}
b_end = getSystemTime();
printf("time = %lu, b_ttl = %lu
", b_end - b_begin, b_ttl);
return 0;
}
执行结果:
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮