473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

cost of object instantiation in .NET 2.0

I'm trying to get a decent idea of the relative performance of three types
of implementations of data-access classes in ASP.NET 2.0.

I believe this boils down to a more basic question regarding the cost of
object instantiation in .NET 2.0. It becomes especially relevant to web-app
data-access classes due to the vast variety of db queries page requests may
require. Caching is not appropriate for most of these datasets, and so many
queries must be performed, implying many data-access object instantiations.

Types of implementations :

1. non-singleton classes with non-static methods where all methods involved
in processing a page request must instantiate various data-access class
objects (possibly multiple times)

2. singleton classes that implement a getInstance() method that provides a
pre-existing instance of the class if one exists; if an instance does not
exist, getInstance() instantiates a class object and assigns it to an
internal static reference

3. classes where all data-provision methods in the interface are implemented
as static methods
In the course of writing a scalable ASP.NET 2.0 web-app, has anyone done any
benchmarking (either formal or informal) ... or has a general sense of the
relative peformance of these 3 implementations ?

Does anyone know of any whitepapers or other studies that might be available
( either MS or external ) ?



May 2 '06 #1
7 2427
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OI**************@TK2MSFTNGP02.phx.gbl...
I'm trying to get a decent idea of the relative performance of three types
of implementations of data-access classes in ASP.NET 2.0.


Wouldn't it be irrelevant compared to retreiving data from a database and
populating a dataset?

Michael
May 2 '06 #2
I'm trying to examine all avenues for peformance optimization. I'm
examining sprocs and ADO.NET code as well as usage of data-access classes.

Cost of instantiation of data-access classes might be small compared with
other costs ... but there is always potential for optimization to provide
value at any app layer.
"Michael C" <no****@nospam.com> wrote in message
news:eR**************@TK2MSFTNGP04.phx.gbl...
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OI**************@TK2MSFTNGP02.phx.gbl...
I'm trying to get a decent idea of the relative performance of three
types of implementations of data-access classes in ASP.NET 2.0.


Wouldn't it be irrelevant compared to retreiving data from a database and
populating a dataset?

Michael

May 2 '06 #3
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote:
I'm trying to get a decent idea of the relative performance of three types
of implementations of data-access classes in ASP.NET 2.0.
If your code is accessing a database, then database access code
(including the network round-trip I assume it's going to involve) is
going to dominate your time, and eliminating as many roundtrips to the
database as possible will be your first, most fruitful, source of
performance increases. That would probably entail bulking together
requests if you can't do any caching.
so many
queries must be performed, implying many data-access object instantiations.
Database calls (implying a network round-trip) are so many times more
expensive than object instantiations that the instantiation cost is in
the realm of a rounding error. I think you need to measure the two costs
before trying too hard to optimize object instantiation.
In the course of writing a scalable ASP.NET 2.0 web-app, has anyone done any
benchmarking (either formal or informal) ... or has a general sense of the
relative peformance of these 3 implementations ?


With respect to memory management on .NET, probably one of the most
important things is to try to achieve a low percentage of time spent on
GC, which in turn means reducing the rate of generation 2 garbage
collections. Keeping gen-2 GCs low implies being aware of how the GC
works, of how large your objects are, and how long you are keeping them
in memory - but you have to measure GC% before you know this is where
your problem is.

The most important thing is to measure: benchmark a 'typical' request
scenario and find out if your time is really going into object
instantiations. When you start talking about the database, I seriously
doubt that micro-optimizing allocations is going to be of much benefit.

Allocating an object in .NET is the cost of incrementing a pointer and
running whatever code is in the constructor - i.e. not much cost at all
and most of it is under your control through the constructor. Objects
get more expensive if you attach them to structures which are going to
live for a while, because that gives them a chance to:

1) Be promoted out of gen-0 into gen-1 (and thus usually out the CPU
cache)
2) or (worst-case scenario) have a mid-life crisis (live until gen-2 and
then become irrelevant)

The shorter your objects' lives with respect to the overall allocation
rate, the better. Sometimes it's cheaper to allocate and initialize a
new object than it is to keep a cached one around, depending on how big
it is and how costly it is to initialize - but measure.

If it's easy to restructure your code not to use allocations, then it
might be worthwhile (but measure first!), but if it means bending over
backwards not to allocate, then it almost certainly isn't worth it. You
should look at your algorithms and actual code running on the hottest
paths first, IMHO.

-- Barry
May 2 '06 #4
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OT**************@TK2MSFTNGP05.phx.gbl...
I'm trying to examine all avenues for peformance optimization. I'm
examining sprocs and ADO.NET code as well as usage of data-access classes.

Cost of instantiation of data-access classes might be small compared with
other costs ... but there is always potential for optimization to provide
value at any app layer.


The difference would have to 1000 to 1 at an absolute minimum. Do whatever
make more sense in your code.

Michael
May 2 '06 #5
John,

You are going to have to instantiate the objects no matter what, it
would seem. The model, as it exists now, does not allow for multithreaded
access to the same objects. For example, you are not going to share the
same connection between threads, nor are you going to share the same data
adapter, or command between threads.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OT**************@TK2MSFTNGP05.phx.gbl...
I'm trying to examine all avenues for peformance optimization. I'm
examining sprocs and ADO.NET code as well as usage of data-access classes.

Cost of instantiation of data-access classes might be small compared with
other costs ... but there is always potential for optimization to provide
value at any app layer.
"Michael C" <no****@nospam.com> wrote in message
news:eR**************@TK2MSFTNGP04.phx.gbl...
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:OI**************@TK2MSFTNGP02.phx.gbl...
I'm trying to get a decent idea of the relative performance of three
types of implementations of data-access classes in ASP.NET 2.0.


Wouldn't it be irrelevant compared to retreiving data from a database and
populating a dataset?

Michael


May 2 '06 #6
"A+", Barry!

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Barry Kelly" wrote:
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote:
I'm trying to get a decent idea of the relative performance of three types
of implementations of data-access classes in ASP.NET 2.0.


If your code is accessing a database, then database access code
(including the network round-trip I assume it's going to involve) is
going to dominate your time, and eliminating as many roundtrips to the
database as possible will be your first, most fruitful, source of
performance increases. That would probably entail bulking together
requests if you can't do any caching.
so many
queries must be performed, implying many data-access object instantiations.


Database calls (implying a network round-trip) are so many times more
expensive than object instantiations that the instantiation cost is in
the realm of a rounding error. I think you need to measure the two costs
before trying too hard to optimize object instantiation.
In the course of writing a scalable ASP.NET 2.0 web-app, has anyone done any
benchmarking (either formal or informal) ... or has a general sense of the
relative peformance of these 3 implementations ?


With respect to memory management on .NET, probably one of the most
important things is to try to achieve a low percentage of time spent on
GC, which in turn means reducing the rate of generation 2 garbage
collections. Keeping gen-2 GCs low implies being aware of how the GC
works, of how large your objects are, and how long you are keeping them
in memory - but you have to measure GC% before you know this is where
your problem is.

The most important thing is to measure: benchmark a 'typical' request
scenario and find out if your time is really going into object
instantiations. When you start talking about the database, I seriously
doubt that micro-optimizing allocations is going to be of much benefit.

Allocating an object in .NET is the cost of incrementing a pointer and
running whatever code is in the constructor - i.e. not much cost at all
and most of it is under your control through the constructor. Objects
get more expensive if you attach them to structures which are going to
live for a while, because that gives them a chance to:

1) Be promoted out of gen-0 into gen-1 (and thus usually out the CPU
cache)
2) or (worst-case scenario) have a mid-life crisis (live until gen-2 and
then become irrelevant)

The shorter your objects' lives with respect to the overall allocation
rate, the better. Sometimes it's cheaper to allocate and initialize a
new object than it is to keep a cached one around, depending on how big
it is and how costly it is to initialize - but measure.

If it's easy to restructure your code not to use allocations, then it
might be worthwhile (but measure first!), but if it means bending over
backwards not to allocate, then it almost certainly isn't worth it. You
should look at your algorithms and actual code running on the hottest
paths first, IMHO.

-- Barry

May 2 '06 #7
The difference in performance would really be minimal between the types
of implementation. I would avoid the second alternative, though. As
ASP.NET is a multi-threaded environment, a singleton class would
introduce more problems than it would solve as you would have to add
locking to make it thread safe.

Keep it simple, and concentrate on stability. Make sure that the
implementation allows you to always handle the database connection
gracefully. If you start leaking connection objects it would cause much
more performance problems than creating a few extra objects.

It doesn't matter how highly optimized the code is, if it doesn't work. :)

John A Grandy wrote:
I'm trying to get a decent idea of the relative performance of three types
of implementations of data-access classes in ASP.NET 2.0.

I believe this boils down to a more basic question regarding the cost of
object instantiation in .NET 2.0. It becomes especially relevant to web-app
data-access classes due to the vast variety of db queries page requests may
require. Caching is not appropriate for most of these datasets, and so many
queries must be performed, implying many data-access object instantiations.

Types of implementations :

1. non-singleton classes with non-static methods where all methods involved
in processing a page request must instantiate various data-access class
objects (possibly multiple times)

2. singleton classes that implement a getInstance() method that provides a
pre-existing instance of the class if one exists; if an instance does not
exist, getInstance() instantiates a class object and assigns it to an
internal static reference

3. classes where all data-provision methods in the interface are implemented
as static methods
In the course of writing a scalable ASP.NET 2.0 web-app, has anyone done any
benchmarking (either formal or informal) ... or has a general sense of the
relative peformance of these 3 implementations ?

Does anyone know of any whitepapers or other studies that might be available
( either MS or external ) ?

May 3 '06 #8

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

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: johny smith | last post by:
Based on my experience there is two ways to instantiate an object. Method 1: Car* car1 = new Car();
11
by: Florian Loitsch | last post by:
I'm currently writing a JS->Scheme compiler (which, using Bigloo, automatically yields a JS->C, JS->JVM, JS->.NET compiler), and have a question concerning the function-parameters: According to...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
2
by: elissa | last post by:
hi guys i really wish if some one could help me with this problem , well i solved it but the output is always wrong so can some one help me: the Question is in this link:...
2
by: WittyGuy | last post by:
Hi My class looks something like this: class Base { public: Base () {} ~Base () {} // No virtual dtor in the base class private: };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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...

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.