静态VS实例方法性能C# [英] Static Vs Instance Method Performance C#

查看:281
本文介绍了静态VS实例方法性能C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 在哪一个更好?静态方法和非静态方法?
  2. 在它之所以好?

<一个href="http://bytes.com/topic/c-sharp/answers/231701-static-vs-non-static-function-performance#post947244">http://bytes.com/topic/c-sharp/answers/231701-static-vs-non-static-function-performance#post947244声明:

由于静态方法是使用锁是线程安全的。该总   在内部做一个Monitor.Enter()和Monitor.exit(),以确保   线程安全的。

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://dotnetperls.com/static-method 规定:

静态方法通常更快来调用比调用堆栈   实例方法。有几个原因,在C#   编程语言。实例方法实际使用的'这个'   实例指针作为第一个参数,这样一个实例方法   总是有这种开销。实例方法还实现了与   所述callvirt指令在中间语言,其中规定一个   轻微的开销。请注意,改变你的方法静态   方法是不太可能帮助不大雄心勃勃的业绩目标,但   它可以帮助一点点,可能导致进一步的削减。

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?

推荐答案

您第一个链接状态:

那是因为静态的方法是使用   锁是线程安全的。总是做   内部一Monitor.Enter()和   Monitor.exit(),以保证线程安全

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


如果添加 [MethodImpl(MethodImplOptions.Synchronized)] 的方法,这种说法成为部分正确。

That is utterly, horribly, abominably wrong.


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

添加此属性将导致CLR包装锁(typeof运算(YourClass))和实例中静态方法里面的锁的方法(这一点)

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

<一个href="http://msdn.microsoft.com/en-us/magazine/cc163896.aspx#ctl00_MTContentSelector1_mainContentContainer_ctl14">This应尽可能避免的

你的第二个环节是正确的。照片 静态方法是有点快于实例方法的,因为他们没有一个参数(因此跳过一个的NullReferenceException 从callvirt指令检查)

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)

这篇关于静态VS实例方法性能C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆