Monday, May 28, 2018

c++ - How can my variable declarations affect execution time

I am a circuit designer (not a software wizard) and have been working on a numerical algorithm for the past 9 months. As a way to evaluate the effectiveness of my algorithms, I monitor the amount of time needed to converge on a solution.




About 6 months ago I discovered that the way I declare my variables can have a dramatic effect on the time needed for the program to run. For example, simply rearranging the declarations, as shown below, can double the time needed to run the code. Simply changing the lengths of the arrays also affects the problem similarly.



int N, j, Iter;
long double RealZero, RealErr, QuadIterErr, QuadX;
long double TUV[3], QuadQP[102], RealQP[102];
bool Updated;

int N, j, Iter;
long double RealZero, RealErr, QuadIterErr, QuadX;
long double QuadQP[102], RealQP[102];

bool Updated;
long double TUV[3];


I initially assumed I had some sort of bug, but I can't find it. Other than speed, I don't see any other anomalies, and I get the same results whether the code runs slow or fast.



I found some discussions on problems associated with packing long doubles, but I didn't understand any of it, and they never said how to fix the problem they were discussing.



Can someone give me some insight as to what might be going on here and how to fix it?




I need consistency more than I need the speed. I am not using any speed optimizers (default compiler settings) and I am using C++ Builder XE3. I am not using a #pragma pack (as someone asked).



Based on the comments, I setup the declarations for slow and fast execution, and compared the base addresses on all the long double variables. Whether slow or fast, the addresses end with a 0, 4, 8, or C.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...