Tuesday, March 31, 2009

Static - Performance Improver Myth



Here the articles covers the ‘Performance Benefits’ of using Static on you code.


Static Classes - Improved Performance


-          Faster Implementation – As the only purpose is to define static members.

-          Static access means that the instantiation of an object is unncessary. The .Net framework Maintains A Single Object In Memory for static members.

-          Use static members whenever possible and only use instantiated class objects whenever more than copy of the object will be necessary to carry the intended function of the project.


Static Constructor - Advantages


-          The static constructor is called just before the first time any instance method or any class method is called.

-          This is also a thread safe way of implementing a singleton, because the class constructors are thread safe in .NET.


public class Singleton
   private static Singleton _instance;
   static Singleton()
     instance = new Singleton();


Static Methods - Improved Performance – FxCop Recommendation


-          For Static Methods, the compiler will Emit Non-Virtual Call to these members which will Prevent A Check At Runtime For Each Call that insures the Current Object Pointer Is Non-Null.

-          This can result in a measurable performance gain for performance-sensitive code.

-          In some cases, the failure to access the current object instance represents a correctness issue. But with Static methods there are no such issues.




Arun Manglick

No comments:

Post a Comment