473,513 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how is performance impact of adding extra methods to a class.

how does it affect performance and memory consumption? i think there will be
no difference memory consumption per instance.
Nov 17 '05 #1
3 1368
The Crow wrote:
how does it affect performance and memory consumption? i think there will be
no difference memory consumption per instance.


Indeed. There'll be a very slight difference in overall memory usage
(obviously the methods have to be loaded *somewhere* into memory), but
that's all. Put it this way: it should never be a deciding factor in
your design.

Jon

Nov 17 '05 #2
to add to Jon's reply. the compiler is able to optimize this so that
there is only one copy of the "instance" method code shared by all
instances of the class. each time the method is entered a new stack
frame is generated to hold any local method state.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
MUS
Hello Crow!

For each object of your type there is only one instance of the methods.
The main problem with virtual functions is performance overhead. As far
as the performance is considered, guys @ MS has given it a lot of
thought that's why you can see that methods in .Net are by default
"non-virtual" as opposed to java where every method is
"virtual" by default.

There is one more thing that is catered using the "non-virtual"
approach and that is versioning but this is alone a separate issue. I
think this
(http://www.parashift.com/c++-faq-lit....html#faq-20.4)
should answer you question.

But you need to keep on eye on one thing that by using only the
"non-virtual" approach you might be sacrificing your design
somewhere at sometime. Let say you have a Style Factory as listed
below:

<code>
public IStyle Create(String type)
{
switch(style)
{
case EMBEDDED_NONE: return new EmbeddedNone();
case EMBEDDED_UNDERSCORE: return new EmbeddedUnderscore();
case EMBEDDED_HYPHENS: return new EmbeddedHyphens();
default: throw new Exception("Unknown Style");
}
}
</code>

If you are using aforementioned approach (i.e. using a non-virtual
factory implementation) than you are downsizing a few design issues
over here. Let say at a latter point you need to add a few more styles
to your repository so then for each new style entry you need to
explicitly change your factory code and mind it with enterprise level
project its not an easy and amusing task at all.

If on the other case you might have use the implementation like this

<code>
protected virtual IStyle Create(String type)
{
switch(style)
{
case EMBEDDED_NONE: return new EmbeddedNone();
case EMBEDDED_UNDERSCORE: return new EmbeddedUnderscore();
case EMBEDDED_HYPHENS: return new EmbeddedHyphens();
default: throw new Exception("Unknown Style");
}
}
</code>

Than at any point in time you can do something like this and this will
give your design a whole new dimension of flexibility and dynamics.

<code>
Protected override IStyle Create(String type)
{
return (IStyle) Activator.CreateInstance(Type.GetType(type));
}
</code>

So remember, "non-virtual" is a GOOD thing to do but it's NOT
always the BEST thing to do.

Hope this might be of some help.

Let me know in case of any inconsistency.

Regards,

Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd

Nov 17 '05 #4

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

Similar topics

8
2458
by: Thomas Junior | last post by:
I always assumed C++ would have better performance than C and, when I had the choice, opted for C++. Today, however, I was researching writing components for Linux and came across this statement: "KDE came first and came earlier to a mature state but C++ makes Qt and MICO particularly inefficient for sophisticated applications. This was...
5
1969
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary, not to get polymorphic behavior. None of them has virtual methods, and are self contained (no destructor at all) thus do not have a chance to have...
14
2028
by: Sugandh Jain | last post by:
Hi, The warning from Microsoft.Performance Code Analysis check that, its not required to initialize numeric variables to zero, boolean to false and object to null is a good one because CLR does it by itself. I wanted to know if one does initialize fields in a class, how much of the performance hit would it have for that same operation...
5
6261
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... I've been trying to improve the performance of our xml handling, so I've been doing some timing tests lately and ran across something I thought odd - XPathDocument seems slightly slower than XmlDocument. I've run the tests several times and have tried to eliminate extraneous variables like disk access but every time, XPathDocument...
0
7269
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7394
Oralloy
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. ...
0
7542
tracyyun
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...
0
5701
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4756
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...
0
3248
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...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
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
0
470
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.