|
-
February 15th, 2004, 08:48 AM
#1
Optimization
Iam dealing with a project that involves code optimization and have a question
on static variables
This is a function that calculates realtime information based on real time data.So this function will
be called as long as we get real time data and the no f times called may be enormous.
Does making these double variables static, improve CPU Performance?
void calculateData()
{
static double dDtScaleMin=m_TimeScaleAttr.GetDataScaleMin();
static double dDtScaleMax=m_TimeScaleAttr.GetDataScaleMax();
static long dtBoundMin=m_TimeScaleAttr.GetDataBoundMin();
static long dtBoundMax=m_TimeScaleAttr.GetDataBoundMax();
static double dtDiffScaleMinMax=dDtScaleMax-dDtScaleMin;
Do some other calculations with the above mentioned double values.
}
-
February 15th, 2004, 09:02 AM
#2
Do these values change? If so, this initialization method is bad because the assignment is only done on the first function call.
The static variables mean that the app doesn't have to allocate stack space every time it enters. Whether this operation (it's one add and one sub instruction per call) noticeably slow down your app is another question. But if the call count is enormous it might well do it.
The only real solution is, write both and measure.
All the buzzt
CornedBee
-
February 15th, 2004, 11:04 PM
#3
optimization
ya the variables are assigned with different values each time it enters the function
-
February 16th, 2004, 12:31 AM
#4
delcaration of static variables
You could declare static variables and assign them seperately.
By this I mean, you could write your code in the following way : -
void calculateData()
{
static double dDtScaleMin;
static double dDtScaleMax;
static long dtBoundMin;
static long dtBoundMax;
static double dtDiffScaleMinMax;
dDtScaleMin = m_TimeScaleAttr.GetDataScaleMin();
dDtScaleMax = m _TimeScaleAttr.GetDataScaleMax();
dtBoundMin = m_TimeScaleAttr.GetDataBoundMin();
dtBoundMax = m_TimeScaleAttr.GetDataBoundMax();
dtDiffScaleMinMax = dDtScaleMax-dDtScaleMin;
// other stuff
...
}
-
February 16th, 2004, 03:54 AM
#5
Re: optimization
Originally posted by kallzz
ya the variables are assigned with different values each time it enters the function
with the code you posted, i dont think they get initialized.
Also looking at the variable it seems that they are to be initialized once and for all.
And thus in that sense it does improve speed, since the reinitialization is avoided.
regards
-
February 17th, 2004, 12:26 AM
#6
optimization
for each 100 msec the m_TimeScaleAttr object changes
void calculateData()
{
static double dDtScaleMin;
dDtScaleMin=m_TimeScaleAttr.GetDataScaleMin();
static double dDtScaleMax;
dDtScaleMax=m_TimeScaleAttr.GetDataScaleMax();
static long dtBoundMin;
dtBoundMin=m_TimeScaleAttr.GetDataBoundMin();
static long dtBoundMax;
dtBoundMax=m_TimeScaleAttr.GetDataBoundMax();
static double dtDiffScaleMinMax;
dtDiffScaleMinMax=dDtScaleMax-dDtScaleMin;
Do some other calculations with the above mentioned double values.
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|