Skip to main content
deleted 58 characters in body; edited tags
Source Link
casperOne
  • 74.7k
  • 19
  • 189
  • 262

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?

Following link shows Non-Static methods are good because, static methods are using locks to be Thread-safe. The always do internally a Monitor.Enter() and Monitor.exit() to ensure Thread-safety.http://bytes.com/topic/c-sharp/answers/231701-static-vs-non-static-function-performance#post947244 states:

http://bytes.com/topic/c-sharp/answers/231701-static-vs-non-static-function-performance because, static methods are using locks to be Thread-safe. The always do internally a Monitor.Enter() and Monitor.exit() to ensure Thread-safety.

And

Following link shows Static Methods are good 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.While http://dotnetperls.com/static-method states:

http://dotnetperls.com/static-method 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?

Thanks

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?

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

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

And

Following link shows Static Methods are good 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.

http://dotnetperls.com/static-method

I am little confused which one to use?

Thanks

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?

edited tags
Link
SLaks
  • 891.3k
  • 182
  • 1.9k
  • 2k

Static Vs Non-StaticInstance Method Performance C#

linkified
Source Link
Michael Myers
  • 192.6k
  • 47
  • 301
  • 297

I have few global methods declared in public class in my aspASP.netNET web application.

I have habbithabit 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 Betterbetter? Static Method or Non-Static Method?
  2. Reason why it is better?

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

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

And

Following link shows Static Methods are good 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.

http://dotnetperls.com/static-methodhttp://dotnetperls.com/static-method

I am little confuseconfused which one to use?

Thanks

I have few global methods declared in public class in my asp.net web application.

I have habbit 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?

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

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

And

Following link shows Static Methods are good 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.

http://dotnetperls.com/static-method

I am little confuse which one to use?

Thanks

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?

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

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

And

Following link shows Static Methods are good 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.

http://dotnetperls.com/static-method

I am little confused which one to use?

Thanks

Source Link
dotnetguts
  • 775
  • 3
  • 7
  • 8
Loading