I have a problem. I am writing data to an array during a loop. And the fact is that I do this very often. This letter seems to now be a bottleneck in code. Since I assume that this is caused by writing to memory. This array is not very big (smth, like 300 elements). The question is, can this be done like this: store it in the cache and update it in memory only after the while loop completes?
[edit - copied from answer added by 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; }
First of all, I would like to thank you all for the answers. Indeed, it was a bit silly not to put code. So I decided to do it now.
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; }
That's all. It would be nice if someone had any ideas. Thanks again.
Regards Alex
c ++ cpu-cache
Alex
source share