473,480 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

static variables and performance

Hi,

I have a a routine that is frequently called. It simply iterates over a
list of dates and does some sanity checking. The following pseudo code
summarises it.

void check()
{
const Date someDate = 1/1/2005;

for (each item)
{
process(item);
}
}

The above routine is called quite frequently.
My question relates to the construction of the Date object above. It's
constructor doesn't incur any great overhead however I'm curious to
know whether I should make it static. By doing that I'll obviously
call its constructor just once and will therefore save some time at
that point, but I'm wondering about the runtime overhead of using a
static? Is there a downside? This isn't a big issue at all, I'm just
curious to know whether it would be better declared as static or
constructed each time I invoke the routine.

Any opinions? Silly question.... there's *always* opinions on this
group :)
thanks and have a nice day

G

Aug 24 '05 #1
10 2745

"Gr*****@nospam.com" <Gr**********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi,

I have a a routine that is frequently called. It simply iterates over a
list of dates and does some sanity checking. The following pseudo code
summarises it.

void check()
{
const Date someDate = 1/1/2005;

for (each item)
{
process(item);
}
}

The above routine is called quite frequently.
My question relates to the construction of the Date object above. It's
constructor doesn't incur any great overhead however I'm curious to
know whether I should make it static. By doing that I'll obviously
call its constructor just once and will therefore save some time at
that point, but I'm wondering about the runtime overhead of using a
static? Is there a downside? This isn't a big issue at all, I'm just
curious to know whether it would be better declared as static or
constructed each time I invoke the routine.

Any opinions? Silly question.... there's *always* opinions on this
group :)
thanks and have a nice day

G


I'm not an expert on static constructors but I think it simply doesn't exist
:-) Or maybe I misunderstood your quest, and let me know if it is true.

What I guess what you are asking is if we can construct something like the
following:

class Date
{
public:
static Date() // constructor
{
// once and for all...
}
};

The above code simply doesn't compile. And also, I don't seem to understand
your pseudo code. Why do you need a Date object to check other Date objects?

Ben
Aug 24 '05 #2
*Ben

May be he meant to make the local 'someDate' static...

Srini

Aug 24 '05 #3
Ian
Gr*****@nospam.com wrote:
Hi,

I have a a routine that is frequently called. It simply iterates over a
list of dates and does some sanity checking. The following pseudo code
summarises it.

void check()
{
const Date someDate = 1/1/2005;

for (each item)
{
process(item);
}
}

The above routine is called quite frequently.
My question relates to the construction of the Date object above. It's
constructor doesn't incur any great overhead however I'm curious to
know whether I should make it static. By doing that I'll obviously
call its constructor just once and will therefore save some time at
that point, but I'm wondering about the runtime overhead of using a
static? Is there a downside? This isn't a big issue at all, I'm just
curious to know whether it would be better declared as static or
constructed each time I invoke the routine.

If it's a constant, why keep constructing it? Use a static.

Ian
Aug 24 '05 #4
In addtion to my previous post:

The sole reason of having a constructor is to hold a validity of a certain
concept. In this case, a valid date shall be established by Date's
constructor, while other member functions help maintain the validity and
defence against unreasonable use. Not doing it results in ineffective design
so that the a date object must be checked from time to time. Keep it simple:

class Date
{
private:
check() throw(Bad_date);

public:
Date();

Date(Year yy, Month mm, Day dd)
{
//...
check();
}

Date(const Date& dt)
{
// don't have to check
// dt is always valid
}

Date& operator = (const Date& dt)
{
// don't have to check
// dt is always valid
}
};
Aug 24 '05 #5
thanks for the replies, I think I should clarify. My question relates
to the *overhead* of having the Date object declared static as opposed
to instantiating one on the stack each time my check() routine is
called.

i.e. should I define;

void check()
{
const Date someDate = 1/1/2005;
// do processing

}

or should I define

void check()
{
static const Date someDate = 1/1/2005;
// do processing

}

In the latter case the Date object constructor is called only once
however I'm interested in understanding the overhead of doing this.
i.e. what overhead am I incurring by declaring it static. The stack
size will increase as the object is left lying around but apart from
that I don't see any other issues. At the same time I feel that using
this approach is not reccommended. Perhaps I'm wrong.

I'm interested in opinions relating to which approach is better?
Graham

Aug 24 '05 #6
thanks for the replies, I think I should clarify. My question relates
to the *overhead* of having the Date object declared static as opposed
to instantiating one on the stack each time my check() routine is
called.

i.e. should I define;

void check()
{
const Date someDate = 1/1/2005;
// do processing

}

or should I define

void check()
{
static const Date someDate = 1/1/2005;
// do processing

}

In the latter case the Date object constructor is called only once
however I'm interested in understanding the overhead of doing this.
i.e. what overhead am I incurring by declaring it static. The stack
size will increase as the object is left lying around but apart from
that I don't see any other issues. At the same time I feel that using
this approach is not reccommended. Perhaps I'm wrong.

I'm interested in opinions relating to which approach is better?
Graham

Aug 24 '05 #7
well that depends on what you are doing in

//do processing.

if you are doing a lot of stuff there as can be inferred from ur first
post, then i doubt that
any substantail savings can be made,

however if this code doses only a bit of processing and most of time is
spent in
calling constructor and destructor of the object , then this can
definatly be made static and you can do away with calling the
constructor and destructor every time you enter this fucntion.

thanks
rt

Aug 24 '05 #8
Ian
Gr*****@nospam.com wrote:

In the latter case the Date object constructor is called only once
however I'm interested in understanding the overhead of doing this.
i.e. what overhead am I incurring by declaring it static. The stack
size will increase as the object is left lying around but apart from
that I don't see any other issues. At the same time I feel that using
this approach is not reccommended. Perhaps I'm wrong.

The stack size will most likely decrease, it makes non sense to have
static objects on the stack.

It is likely to end up somewhere else, exactly where depends on the
implementation.

There is no net overhead, the object has to exist somewhere (assuming it
isn't optimised away).

Ian
Aug 24 '05 #9
Gr*****@nospam.com schreef:
thanks for the replies, I think I should clarify. My question relates
to the *overhead* of having the Date object declared static as opposed
to instantiating one on the stack each time my check() routine is
called. .... In the latter case the Date object constructor is called only once
however I'm interested in understanding the overhead of doing this.
i.e. what overhead am I incurring by declaring it static. The stack
size will increase as the object is left lying around


(disclaimer: C++ implementations may differ)
Static variables usually aren't allocated on the stack. This would be
a problem, as the stack memory is released when returning from a
function. The solution is to allocate memory from some global pool,
and call a ctor on that memory once the static is constructed. Of
course, an optimizer might detect that the result is predictable and
optimize it away.
But the basic tradeoff is there: speed vs memory.

HTH,
Michiel Salters

Aug 24 '05 #10
thanks for those replies and clarifications. I've gone with static as
there isn't a lot of processing being done.

cheers, G

Aug 24 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
6338
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
115
7459
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
9
4593
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
16
2513
by: Bruno Rodrigues | last post by:
Hi In a class with simple methods like: Products.Insert(string name, string desc) Products.Update(int id, string name, string desc) Products.Delete(int id) What is better? Static or common...
4
1852
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
8
4844
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
28
4571
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
9
2149
by: Pohihihi | last post by:
What could be the possible reasons (technical/non technical) of not using lots of static functions or variables in a program keeping in mind that Framework by itself has tons of static functions and...
2
2229
by: james | last post by:
Hi I am considering storing my session variables within one static object with session scope. The static object will be a class with accessor functions to get and set the equivalent session...
9
8325
by: Allen | last post by:
In a static library, there is a static variable definition. static CLogger::mapFile; In both EXE and DLL, I use the same code. CLogReader uses CLogger::mapFile to do some work. CLogReader...
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7081
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4776
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.