我有一个问题....我正在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;
}
Run Code Online (Sandbox Code Playgroud)
首先,我要感谢你们所有人的回答.实际上,不放置代码有点蠢.所以我现在决定这样做.
double* array1 = new double[1000000]; // this …Run Code Online (Sandbox Code Playgroud)