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 10 2775
"Gr*****@nospam .com" <Gr**********@g mail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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
*Ben
May be he meant to make the local 'someDate' static...
Srini 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
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
}
};
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
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
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 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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 keyword is useful
inside struct, class, and function only unless you want to force local
variable to be global variable so static is used.
Do you have idea why most programmers do this?
Bryan Parkoff
|
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 transform function.
When compiling under gcc on my big-endian PowerPC (Mac OS X),
declaring this array as "static" DECREASES the transform throughput by
around 5%. However, declaring it as "static" on gcc/Linux/Intel
INCREASES the throughput by...
|
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?
|
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 methods? This objects do not need
instances.
|
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
"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfstaticpg.asp".
I opened up two instances of my browser to simulate two users accessing the
page at the same time by pointing to Employee.aspx below. Refreshing several
times, the counter value is increased and shared between both browser...
| |
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 differences in the way both the objects are
handled by IIS.
Are both objects stored in different memory spaces?
I can access both the objects in my web page.
I will be grateful if some one can help me understand the difference.
|
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. Would the performance be better
using Static versus Dynamic. I would think it would be quicker with STATIC
declarations since the variables would only have to be created once. Can
anyone confirm this. Thanks.
--
Dennis in Houston
|
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 variables?
|
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 variables stored as
member variables. This will allow strong typing. Apart from the
problems of multithreading, are there any performance overheards of
which I should be aware?
Thanks
|
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 logReader;
But the result is different.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |