473,890 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11172
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.co m

"Marc Gravell" <ma**********@g mail.com> wrote in message
news:eQ******** ******@TK2MSFTN GP04.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

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

Similar topics

1
3890
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 restricts a couple of columns to having values within the values assigned in my application by an enumeration: (( >= 0 and <=3) and ( >= 0 and <= 2)) This enforces domain integrity for two enumerations having values 0, 1, 2, 3
115
7689
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...
3
3084
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) in this method. This method can be executing at multiple times, but on different threads. All the dnsClient.Lookup method does is resolve a hostname to an IP Address. It does not modify any other variables or anything. The reason why I made...
6
1927
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 class needs the session context to encode a url (because I stored the current language there), so I made a static field of type HttpContext, which I refresh every reqest by assigning the current context. Now, every now and then I get this...
8
8942
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. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
9
6101
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
1373
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 dispName,string sExtra); }
2
1659
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 of objects can be different for different users. This method uses some non-static variables which are local to this method. Following is my static class:
13
20793
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 & I don't want to copy + paste code. How can I inherit those member variables? Thanks
0
9975
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11212
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10799
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10899
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,...
1
8004
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 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...
0
7154
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();...
0
5832
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...
0
6032
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3263
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.