19

I have few global methods declared in public class in my ASP.NET web application.

I have habit of declaring all global methods in public class in following format

public static string MethodName(parameters) { }

I want to know how it would impact on performance point of view?

  1. Which one is better? Static Method or Non-Static Method?
  2. Reason why it is better?

http://bytes.com/topic/c-sharp/answers/231701-static-vs-non-static-function-performance#post947244 states:

because, static methods are using locks to be Thread-safe. The always do internally a Monitor.Enter() and Monitor.exit() to ensure Thread-safety.

While http://dotnetperls.com/static-method states:

static methods are normally faster to invoke on the call stack than instance methods. There are several reasons for this in the C# programming language. Instance methods actually use the 'this' instance pointer as the first parameter, so an instance method will always have that overhead. Instance methods are also implemented with the callvirt instruction in the intermediate language, which imposes a slight overhead. Please note that changing your methods to static methods is unlikely to help much on ambitious performance goals, but it can help a tiny bit and possibly lead to further reductions.

I am little confused which one to use?

casperOne
  • 73,706
  • 19
  • 184
  • 253
dotnetguts
  • 735
  • 3
  • 7
  • 8
  • Did you read to the end of that first link? It becomes quite clear, even within the thread, that the assertion about automatic locking is false. – Dan Tao Jun 10 '10 at 17:24

4 Answers4

46

Your first link states:

Thats because static methods are using locks to be Thread-safe. The always do internally a Monitor.Enter() and Monitor.exit() to ensure Thread-safety

That is utterly, horribly, abominably wrong.


If you add [MethodImpl(MethodImplOptions.Synchronized)] to the method, that statement becomes partially true.

Adding this attribute will cause the CLR to wrap static methods inside lock(typeof(YourClass)) and instance methods inside of lock(this).

This should be avoided where possible


Your second link is correct.
Static methods are a little bit faster than instance methods, because they don't have a this parameter (thus skipping a NullReferenceException check from the callvirt instruction)

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
7

I tend to care very little about performance in this respect. What static methods are really useful for are enforcing functional practices. For example if you create a private static helper method in your instance class you have the piece of mind knowing that that method cannot modify the state of the instance.

ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
2

I personally would always choose the approach that is better for achiving your current task and write stable, readable and easy to maintain code.

There are other ways to improve performance of your application.

some examples:

  • If you want to use a simple method multiple times without instancing an object every time (a helper function) then use a static method in a static class.

  • If your method accesses other variables in the class and is not thread safe use s member function.

  • In asp.net if you want to share an object accross sessions or you can improve performance with a method that internally caches the result a static method would be fine, too.

  • You can mix both ways and use the factory design pattern to have a class with some member functions, but you ensure that there is always only one instance at a time.

  • Sometimes a static function can avoid stupid errors or reduces the need of additional runtime checks:

    String.IsNullOrEmpty(thisstringisnull)  // returns true
    thisstringisnull.IsNullOrEmpty() // If Microsoft would have implemented
                                     // the method this way you would get a
                                     // NullReferenceException
    

But overall it totally depends on the current task. There's no easy "always use this approach..." answer to your question.

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • Thanks SchlaWiener for answering. I declare utility methods which are used many times and are good from maintainable approach, but I was confused of locking of method as mentioned in one thread. – dotnetguts Jun 10 '10 at 17:41
  • Actually you can have extension methods that work with null values now, so thisstringisnull.IsNullOrEmpty() could make sense. – SWeko Jun 10 '10 at 17:57
  • @SWeko: really? didn't know about that. Is that true for every extension method or I need to add some extra code if I write an extension method? – Jürgen Steinblock Jun 10 '10 at 20:07
  • Well, extension methods are just syntaxic sugar, other than the look of the call site, there's nothing special about that first parameter. So yeah, this works for any extension method. – SWeko Jun 10 '10 at 20:18
0

This is basically a design choice. If you have logic which include creating instance of class and updating some properties, go for instance method as static method will be shared across the instances. While if you have some utility functions like doing some string manipulation, creating a connection string etc.. which doesn't involve object manipulation, go for static method.

Chinjoo
  • 2,697
  • 6
  • 28
  • 45