arrays - Best way to optimize performance of program with arrays that regularly need to be reset to zero in C?
问题描述
I have searched to see if this question has been asked before, but I could not find anything - but when I was searching I found some many interesting points about optimization in this answer and the other answers to a question about optimization.
My question is to ask which way is the most efficient/ fastest to set the elements of a large array to zero using C.
The program will track a large number of particles, >>>1000. Each particle is described by several variables some of which will need to be reset to zero every time around a loop, which will be executed >>>1000 times. The exact number of particles that can be handled will depend on the efficiency of the code.
The choices seem to be the following and I have ordered them as I guess from least efficient to most efficient. (I try to describe them with indicative code fragments - no way, of course, is this code that can run, but just something to indicate the strategy - and I realise that loop unrolling might be a good idea, but for simplicity it is not included below)
- N particles are represented by an array of a structure that contains all the information about each particle so
/*structure definition*/
struct particle {
double a;
double b;
....
};
/*memory allocation*/
struct particle * part;
part = (struct particle *)calloc(N,sizeof(particle));
/*routine to set some particle variables to zero*/
for (i=0;i<N;i++)
{
part[i].a=0;
part[i].b=0;
.... etc....
}
- N particles are represented by several arrays in a structure that contains all the information about the ensemble of particles so
/*structure definition*/
struct ensemble {
double * a;
double * b;
....
};
/*memory allocation*/
struct ensemble group;
group.a = (double *)calloc(N,sizeof(double));
group.b = (double *)calloc(N,sizeof(double));
/*routine to set some particle variables to zero*/
for (i=0;i<N;i++)
{
group.a[i]=0;
group.b[i]=0;
.... etc....
}
- exactly the same as 2) above but the variables are reset to zero with
/*routine to set some particle variables to zero*/
free(group.a); group.a = (double *)calloc(N,sizeof(double));
free(group.b); group.b = (double *)calloc(N,sizeof(double));
instinctively I think there must be an easier way to than 3) to write 0 to memory, which does not require freeing and then reallocating large amounts of memory every time around the loop. -- The answers to this question mention
memset
, which i am guessing would work, provided that setting everything to zero bytewise will give doubles with values of0.0000000e00
.same as 2), 3), 4) above, but instead of using any data structure just grab memory for separate arrays.
/*memory allocation*/
double * a, * b, ... ;
a = (double *)calloc(N,sizeof(double));
b = (double *)calloc(N,sizeof(double));
/*routine to set some particle variables to zero*/
for (i=0;i<N;i++)
{
a[i]=0;
b[i]=0;
.... etc....
}
Finally, I saw something that *(a+i)=0
would be quicker than a[i]=0
, but for readability the code above has a[i]
array indexing.
I also guess that it may be the compiler with optimization flags turned on will do some of these things.
I would be really interested to hear what would be expected to be fastest and how much improvement might be obtained in each refinement..
解决方案
推荐阅读
- php - 是否可以将 $_SESSION 变量保存到数组中?
- python - 试图让 tensorflow 2 与 WSL 2 一起工作时遇到问题
- javascript - 记忆游戏翻转单张卡片不起作用 - JavaScript
- android - 过滤编号的 ArrayList
包含 android 中的重复项 - pipenv - 如何使用 && 使用 pipenv 脚本运行两个命令?
- bash - 当数字相等时,为什么我的 bash less then 语句评估为 TRUE?
- javascript - 为什么我的 javascript 只执行第一行?Prestashop
- flutter - 滚动时 Listview 抛出错误
- javascript - 使用 knexjs 检测并发出 onConflict 信号
- css - 引导模式页脚中的水平线超出窗口