473,387 Members | 1,493 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,387 software developers and data experts.

Does Static class affect performance of ASP.NET site

Hai folks,

I have a question to get exact answer from you people. My question is

How Static class is differ from instance class and If you use static
class in ASP.NET, ll it affect speed or performance of site...?

Because I am using static class in my Database layer. But one of my
friend told me that "static class ll take more space than instance
class and it ll affect the performance. But if you use Instance class
you can create object and you can destroy that object, so that
performance ll be good." Is that true..? If so how... I want the proper
solution for this.

Thanks in advance
Raja Chandrasekaran

Jun 6 '06 #1
11 11116
It depends largely on what resides *on* the class; if this uses stateless
methods (i.e. no static fields are updated), then I don't see any problem
with static - in fact it removes the need to even create any objects (per
call) [although in any useful example this is going to be so trivially
minimal impact].

AVOID using stateful static properties / fields on such classes though;
remember that many threads can be talking to the class, so either you will
get thread-race bugs when multiple clients are connecting, or you'll
wide-spread blocking - essentially yielding only 1 useful thread at a time.

So:
* stateless static should be fine
* stateful instance should be fine
* performance isn't really a factor unless these are mammoth classes, in
which case you have bigger problems

Marc
Jun 6 '06 #2
> i.e. no static fields are updated
should have read
"no static fields are used"
Jun 6 '06 #3

Marc Gravell wrote:
i.e. no static fields are updated

should have read
"no static fields are used"


Thanks for comment.its fine. But If I use Normal class with static
members and functions... what ll be the advantages and disadvantages...
Y I am aasking to increase the performance of my application by all the
ways...

Jun 6 '06 #4
for this scenario, any static members should be stateless

Your colleagure wouldn't be confusing this with the VB6 world, would they? A
..Net static class does *not* mean that an instance is created and re-used
for multiple callers. Quite the opposite. There /is/ no instance ("there is
no spoon"); the methods are accessed via the type-definition directly. This
means that there is no need to instantiate or GC any instances, which should
theoretically lead to increased performance (I haven't qualified this, as it
would need some digging). However, the difference is going to be so marginal
(compared to everything else happening) that I would opt for whatever keeps
the system simplest.

That being said - there is never any purpose in having non-static stateless
methods (in fact, FxCop used to whinge at me for doing this...).

The real question is: is my usage stateless? Or do I rely on any static
fields / whatever. If your methods are stateless, then go static: you will
save a few lines of code and get some minor performance hits; nothing to get
excited over, though. If your methods are *not* stateless, then go instance.

Marc
Jun 6 '06 #5
Marc,

I disagree with the statement that static members should be stateless.
It is very possible that the design of the static class calls for it to be
stateful. This would be done when representing something that only one
instance exists for the duration of the program's execution (for example,
Application in ASP.NET would be completely viable as a static class, in my
opinion). Of course, one could make the argument that in this case a
singleton is preferable, since you could pass the singleton instance around.

Also, there is a purpose for non-static stateless methods. In COM+, for
example, if you were using transactions or JIT-activated implementations,
your design pretty much forced your instance methods to be stateless. Even
now, using remoting, your server's methods should be stateless in certain
activation scenarios (single-call, for example).

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

"Marc Gravell" <ma**********@gmail.com> wrote in message
news:eQ**************@TK2MSFTNGP04.phx.gbl...
for this scenario, any static members should be stateless

Your colleagure wouldn't be confusing this with the VB6 world, would they?
A .Net static class does *not* mean that an instance is created and
re-used for multiple callers. Quite the opposite. There /is/ no instance
("there is no spoon"); the methods are accessed via the type-definition
directly. This means that there is no need to instantiate or GC any
instances, which should theoretically lead to increased performance (I
haven't qualified this, as it would need some digging). However, the
difference is going to be so marginal (compared to everything else
happening) that I would opt for whatever keeps the system simplest.

That being said - there is never any purpose in having non-static
stateless methods (in fact, FxCop used to whinge at me for doing this...).

The real question is: is my usage stateless? Or do I rely on any static
fields / whatever. If your methods are stateless, then go static: you will
save a few lines of code and get some minor performance hits; nothing to
get excited over, though. If your methods are *not* stateless, then go
instance.

Marc

Jun 6 '06 #6
First - I'm not disagreeing with the previous poinnt.

OK, let me revise my statement then: stateful static methods should be very,
very careful about thread-safety; minimised lock times, that sort of thing.
I do, indeed, make use of stateful static classes, but you need to be
careful.

In the given (DAL) scenario, you could probably go a little either way
(depending on what exactly is meant); however, if one of the static fields
is the connection, then you are effectively forcing all your requests down a
single pipe - not ideal. It would be more efficient to let .Net pooling deal
with keeping the connections, and get on with writing the rest of the app.

Other differentiating features: static means you can't really use
inheritance, and you can only use interfaces via a singleton.

Marc

Jun 6 '06 #7
Raja,
To get back to the main point of your post (aside from the valid points
raised about being careful with static Fields carrying state) there should be
no perceptible performance difference between a static method and an
identical instance method.
Peter

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


"Raja Chandrasekaran" wrote:

Marc Gravell wrote:
i.e. no static fields are updated

should have read
"no static fields are used"


Thanks for comment.its fine. But If I use Normal class with static
members and functions... what ll be the advantages and disadvantages...
Y I am aasking to increase the performance of my application by all the
ways...

Jun 6 '06 #8
Peter Bromberg [C# MVP] wrote:
Raja,
To get back to the main point of your post (aside from the valid points
raised about being careful with static Fields carrying state) there
should be no perceptible performance difference between a static method
and an identical instance method.


If you are just talking about the method itself, then that is true. If you
are talking about total execution, then there is not enough information to
provide an answer. How much time does it take to create an instance of the
object? For example, is the object configurable, and it reads the
configuration from an external source? If so, creating an instance of the
object may take a perceptible amount of time, time that can be avoided or
greatly limited by using a different pattern.
--
Tom Porterfield

Jun 6 '06 #9
Tom,
Good point. This is not something I would spend my time writing tests for.
But, from what I have seen, in either case, the assembly must be loaded.
Object instantiation is probably the least time consuming operation in the
whole scheme of things, the CLR can instantiate 10 million objects in a
second.

In the IL, typicially the static method will be using the calll and the
instance method the callvirt instruction.

Callvirt does null checking of the object before it tries to invoke the
method.
So in that regard, static methods will be faster than instance methods,
since there is no null checking.

Again, the difference is so small as to be inconsequential, IMHO.

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


"Tom Porterfield" wrote:
Peter Bromberg [C# MVP] wrote:
Raja,
To get back to the main point of your post (aside from the valid points
raised about being careful with static Fields carrying state) there
should be no perceptible performance difference between a static method
and an identical instance method.


If you are just talking about the method itself, then that is true. If you
are talking about total execution, then there is not enough information to
provide an answer. How much time does it take to create an instance of the
object? For example, is the object configurable, and it reads the
configuration from an external source? If so, creating an instance of the
object may take a perceptible amount of time, time that can be avoided or
greatly limited by using a different pattern.
--
Tom Porterfield

Jun 6 '06 #10
Thank you folks...

So there is no performance diff... But what about the memory issue...
Is there any diff... So Can I use any number of static classes or
methods... Or which situation is the best to use static method or class
and which is best for instance class...

Jun 7 '06 #11
"Raja Chandrasekaran" <Ra*****@gmail.com> wrote:
So there is no performance diff... But what about the memory issue...
Is there any diff... So Can I use any number of static classes or
methods... Or which situation is the best to use static method or class
and which is best for instance class...


Create an instance class if you need multiple instances with private
data per instance.

Create static properties when any shared state needs to be global.

Create static methods when the return value depends only on static
properties, static fields or the arguments.

Use a static class if there is no instance members in the class, only
static members.

-- Barry

--
http://barrkel.blogspot.com/
Jun 7 '06 #12

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

Similar topics

1
by: Robin Tucker | last post by:
I'm considering adding domain integrity checks to some of my database table items. How does adding such constraints affect SQL Server performance? For example, I have a simple constraint that...
115
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...
3
by: Amy L. | last post by:
I have a method below called "ResolveHostname" which is called from the ThreadPool.QueueUserWorkItem. My concern is with the static dnsClient class where I am calling dnsClient.Lookup( Hostname)...
6
by: Vladislav Kosev | last post by:
I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
9
by: pigeonrandle | last post by:
Hi, Can anyone tell me why i cant create the following class as 'static': //Start using System; namespace WinAppSQL.windows { public static class WinAPI
5
by: intrader | last post by:
The code is: <code> namespace ConsoleApp { public interface Ivalidator{ String ErrorMessage{get;} Boolean IsValid{get;} void Validate(string value,long mask, long maxLength,string...
2
by: Anup Daware | last post by:
Hi Group, I have a little confusion over the use of static class in C#. I have a static method in my static class. This method reads an xml and returns a collection of objects. This collection...
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.