我有一个问题....我在while循环中将数据写入数组。关键是我经常这样做。看来,这种写作现在是代码中的瓶颈。所以我认为这是由写入内存引起的。这个数组并不是很大(比如 300 个元素)。问题是可以这样做:仅在while循环完成后将其存储在缓存中并在内存中更新?
[编辑 - 复制自 Alex 添加的答案]
double* array1 = new double[1000000]; // this array has elements
unsigned long* array2 = unsigned long[300];
double varX,t,sum=0;
int iter=0,i=0;
while(i<=max_steps)
{
varX+=difX;
nm0 = int(varX);
if(nm1!=nm0)
{
array2[iter] = nm0; // if you comment this string application works more then 2 times faster :)
nm1=nm0;
t = array1[nm0]; // if you comment this string , there is almost no change in time
++iter;
}
sum+=t;
++i;
}
首先我要感谢大家的回答。确实,不输入代码有点愚蠢。所以我决定现在就去做。
double* array1 = new double[1000000]; // this array has elements
unsigned long* array2 = unsigned long[300];
double varX,t,sum=0;
int iter=0,i=0;
while(i<=max_steps)
{
varX+=difX;
nm0 = int(varX);
if(nm1!=nm0)
{
array2[iter] = nm0; // if you comment this string application works more then 2 times faster :)
nm1=nm0;
t = array1[nm0]; // if you comment this string , there is almost no change in time
++iter;
}
sum+=t;
++i;
}
就是这样。如果有人有任何想法,那就太好了。再一次非常感谢你。
真诚的亚历克斯