C循环优化对最终分配的帮助因此,在我的计算机系统类的最后一次作业中,我们需要优化这些循环,使其比原来的更快。在我们的Linux服务器上,基本等级是7秒以下,完整级别是5秒以下。这里的代码大约有5.6秒。我想我可能需要在某种程度上用指针来让它更快,但我不太确定。有人能给我一些建议或选择吗?非常感谢!QUICKEDIT:文件必须保持50行或更少,我忽略了指导员包含的那些注释行。#include <stdio.h>#include <stdlib.h>// You are only allowed to make changes to this code as specified by the comments in it.// The code you submit must have these two values.#define N_TIMES 600000#define ARRAY_SIZE 10000int main(void){
double *array = calloc(ARRAY_SIZE, sizeof(double));
double sum = 0;
int i;
// You can add variables between this comment ...
register double sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0, sum8 = 0, sum9 = 0;
register int j;
// ... and this one.
printf("CS201 - Asgmt 4 - \n");
for (i = 0; i < N_TIMES; i++)
{
// You can change anything between this comment ...
for (j = 0; j < ARRAY_SIZE; j += 10)
{
sum += array[j];
sum1 += array[j + 1];
sum2 += array[j + 2];
sum3 += array[j + 3];
sum4 += array[j + 4];
sum5 += array[j + 5];
sum6 += array[j + 6];
sum7 += array[j + 7];
sum8 += array[j + 8];
sum9 += array[j + 9];
}
// ... and this one. But your inner loop must do the same
// number of additions as this one does.
}
// You can add some final code between this comment ...
sum += sum1 + sum2 + sum3 + sum4 + sum5 + sum6 + sum7 + sum8 + sum9;
// ... and this one.
return 0;}
3 回答
MMMHUHU
TA贡献1834条经验 获得超8个赞
- 3 回答
- 0 关注
- 398 浏览
添加回答
举报
0/150
提交
取消