因为算法本身需要在函数内生成一组值为0的数组,然后反复计算赋值,数组比较大,循环次数又比较多,所以我想每次是否需要很长时间来calloc我的想法是能否从外部直接调用main函数里定义的值为0的数组,这样计算速度会不会变快。
void function(double a[rxy],double R[rxy],double FR[rxy]){
int p,q;
double * cc;
cc = (double*)calloc(rxy,sizeof(double));
double * fr;
fr = (double*)calloc(rxy,sizeof(double));
for(p = 0;p <= Lx;++p){
for(q = 0;q <=Ly;++q){
for(int k = 0;k <= Lx - p;++k){
for(int l = 0;l <= Ly - q;++l){
fr[p * (Ly + 1) + q] = a[k * (Ly + 1) + l] * a[k * (Ly + 1) + l + p * (Ly + 1) + q];
cc[p * (Ly + 1) + q] = cc[p * (Ly + 1) + q] + fr[p * (Ly + 1) + q];
}
}
cc[p * (Ly + 1) + q] = cc[p * (Ly + 1) + q] - R[p * (Ly + 1) + q];
}
}
for(int i = 0;i <= Lx;++i){
for(int j = 0;j <= Ly;++j){
FR[i * (Ly + 1) + j] = cc[i * (Ly + 1) + j];
}
}
free(cc);free(fr);
}
就是我想把前几句的calloc在外部就定义好,然后作为参数传递给函数,而不改变其值,使得以后的函数调用还是初始化的数组。