静态与实例方法性能C#

时间:2022-02-01 16:54:35

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

我在ASP.NET Web应用程序中的公共类中声明了很少的全局方法。

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. 哪一个更好?静态方法还是非静态方法?
  3. Reason why it is better?
  4. 为什么它更好?

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#post947244指出:

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

因为,静态方法使用锁是线程安全的。始终在内部执行Monitor.Enter()和Monitor.exit()以确保线程安全。

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.

静态方法在调用堆栈上调用通常比实例方法更快。在C#编程语言中有几个原因。实例方法实际上使用'this'实例指针作为第一个参数,因此实例方法将始终具有该开销。实例方法也使用中间语言的callvirt指令实现,这会产生轻微的开销。请注意,将方法更改为静态方法不太可能对雄心勃勃的性能目标有所帮助,但它可以帮助一点点,并可能导致进一步减少。

I am little confused which one to use?

我有点困惑哪一个使用?

4 个解决方案

#1


43  

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

这是因为静态方法使用锁是线程安全的。始终在内部执行Monitor.Enter()和Monitor.exit()以确保线程安全

That is utterly, horribly, abominably wrong.


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

如果向方法添加[MethodImpl(MethodImplOptions.Synchronized)],则该语句将变为部分为真。

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

添加此属性将导致CLR将lock(typeof(YourClass))中的静态方法和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)

你的第二个链接是正确的。静态方法比实例方法快一点,因为它们没有this参数(因此从callvirt指令跳过NullReferenceException检查)

#2


5  

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.

在这方面,我倾向于很少关注表现。哪些静态方法对于实施功能实践非常有用。例如,如果在实例类中创建私有静态助手方法,则可以知道该方法无法修改实例的状态。

#3


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.

    如果您的方法访问类中的其他变量并且不是线程安全的,请使用s成员函数。

  • 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.

    在asp.net中,如果你想在会话*享一个对象,或者你可以通过内部缓存结果的方法来提高性能,那么静态方法也可以。

  • 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.

但总体而言,这完全取决于当前的任务。没有简单的“总是使用这种方法......”回答你的问题。

#4


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.

这基本上是一种设计选择。如果您有包含创建类实例和更新某些属性的逻辑,请转到实例方法,因为静态方法将在实例之间共享。虽然如果你有一些实用函数,比如做一些字符串操作,创建一个连接字符串等......不涉及对象操作,那就去静态方法。

#1


43  

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

这是因为静态方法使用锁是线程安全的。始终在内部执行Monitor.Enter()和Monitor.exit()以确保线程安全

That is utterly, horribly, abominably wrong.


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

如果向方法添加[MethodImpl(MethodImplOptions.Synchronized)],则该语句将变为部分为真。

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

添加此属性将导致CLR将lock(typeof(YourClass))中的静态方法和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)

你的第二个链接是正确的。静态方法比实例方法快一点,因为它们没有this参数(因此从callvirt指令跳过NullReferenceException检查)

#2


5  

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.

在这方面,我倾向于很少关注表现。哪些静态方法对于实施功能实践非常有用。例如,如果在实例类中创建私有静态助手方法,则可以知道该方法无法修改实例的状态。

#3


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.

    如果您的方法访问类中的其他变量并且不是线程安全的,请使用s成员函数。

  • 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.

    在asp.net中,如果你想在会话*享一个对象,或者你可以通过内部缓存结果的方法来提高性能,那么静态方法也可以。

  • 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.

但总体而言,这完全取决于当前的任务。没有简单的“总是使用这种方法......”回答你的问题。

#4


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.

这基本上是一种设计选择。如果您有包含创建类实例和更新某些属性的逻辑,请转到实例方法,因为静态方法将在实例之间共享。虽然如果你有一些实用函数,比如做一些字符串操作,创建一个连接字符串等......不涉及对象操作,那就去静态方法。