静态方法VS实例方法性能 [英] Performance of static methods vs instance methods

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

问题描述

我的问题是有关的静态方法VS实例方法和它们的可扩展性的性能特点。承担这种情况下,所有的类定义是在一个单一的组装和所需的多个离散的指针类型。

考虑:

 公共密封类InstanceClass
{
      公众诠释DoOperation1(字符串输入)
      {
          //一些操作。
      }      公众诠释DoOperation2(字符串输入)
      {
          //一些操作。
      }      // ...更多实例方法。
}公共静态类StaticClass
{
      公共静态INT DoOperation1(字符串输入)
      {
          //一些操作。
      }      公共静态INT DoOperation2(字符串输入)
      {
          //一些操作。
      }      // ...更多静态方法。
}

以上班重新present帮手风格的图案。

在一个实例类,解决实例方法花点时间做,因为反对StaticClass。

我的问题是:


  1. 在保持状态不是一个问题(不需要字段或属性),是它总是最好使用一个静态类?


  2. 哪里有相当数量的这些静态类定义的(说100例如,拥有一批各静态方法),这会不会影响执行性能和内存消耗负,与相同数量的实例类的定义比较


  3. 在同一个实例类中的另一个方法被调用,并实例分辨率仍时有发生?例如使用[这]关键字如 this.DoOperation2(ABC)从内部 DoOperation1 同一个实例的



解决方案

在理论上,一个静态方法应该执行比一个实例方法稍微好一点,其他所有的事情都是因为额外的隐藏这种平等, 参数。

在实践中,这使得这样的小差别,它会被隐藏在各种编译器决定的噪声。 (因此,两个人可以证明比另一个更好地与不同意的结果)。尤其是因为在这个通常在寄存器中传递,往往是在注册的开始。

这最后一点意味着,在理论上,我们应该期待接受一个对象作为参数,并做一些事情与它要稍微比同等作为同一对象的实例不太好的一个静态方法。再次,虽然,差异非常轻微,如果你试图测量它你可能最终测量一些其他的编译器的决定。 (特别是自可能性如果引用一个注册的全部时间是相当高太)。

真正的性能差异将下降到您是否已经得到了人为在内存中做一些事情,自然应该是静态的物体,或者你纠缠起来链对象传递以复杂的方式做什么自然应该实例

因此​​,对于数1.保持状态不是一个问题,它总是最好是静态的,因为这是静态的是。这不是一个性能问题,虽然有与编译器的优化很好地发挥整体的规则 - 它更可能是有人去优化该拿出比那些拿出怪用正常使用情况下的功夫

2号。没什么区别。有每个成员一定量的每个类的成本是两个有多少元数据,多少code有实际DLL或EXE文件,以及有多少即时编译code就会有条款。这是相同的无论是实例或静态的。

通过3项,这个这个一样。但是请注意:


  1. 这个参数在一个特定的寄存器传递。当调用同一个类中的一个实例方法,它很可能会成为该寄存器已经(除非它被藏匿,并用于某些原因寄存器),因此没有设置这个必要的行动来它需要被设置到什么。这适用于在一定程度上与例如前两个参数的方法是它使一个呼叫的前两个参数


  2. 由于这将是清楚的是这个不为空,这可能是用于优化在某些情况下调用。


  3. 由于这将是清楚的是这个不为空,这可能使内联方法调用,更高效的再次,作为code到生产假方法调用可以省略一些空的检查可能需要无妨。


  4. 这是说,null检查很便宜!


有值得注意的是,作用在物体上,而不是实例方法一般静态方法,可以减少一些在<一个讨论的费用href=\"http://www.bluebytesoftware.com/blog/2011/10/23/OnGenericsAndSomeOfTheAssociatedOverheads.aspx\">http://www.bluebytesoftware.com/blog/2011/10/23/OnGenericsAndSomeOfTheAssociatedOverheads.aspx在的情况下,考虑静态不叫给定类型。正如他所说的那样顺便说一句,事实证明,扩展方法是使通用的抽象一个伟大的方式更付费的发挥。

然而,注意,这仅涉及由该方法使用其它类型的,即不另外存在的实例。因此,它真的并不适用于很多情况下(其他一些实例方法使用的类型,其他一些code别的地方使用的类型)。

摘要:


  1. 晴实例的性能成本VS静态低于忽略不计。

  2. 哪些费用通常有会来,你滥用静态比如,反之亦然。如果你不让它静态和实例之间你的决定的一部分,你更有可能得到正确的结果。

  3. 有些情况下被创造在种类较少另一种结果静态泛型方法罕见的情况下,比例如通用的方法,可以使有时有小利把很少使用(和很少是指哪些类型它在应用中的寿命与使用时,它不被调用多少)。一旦你得到了什么,他的那篇文章中谈论你会看到,它是100%无关大多数静态-VS实例决定无论如何。编辑:它大多只具有NGEN的成本,而不是与即时编译code

编辑:在空的检查是多么便宜的说明(我在上面声称)。在.NET中最空的检查不检查空可言,而他们继续他们打算与假设,它会努力做的,如果访问异常情况发生它就会变成一个的NullReferenceException 。因此,大多时在概念上的C#code涉及空检查,因为它是在访问一个实例成员,如果成功的成本实际上是零。一个例外是一些内置调用,(因为他们要表现得好像他们所谓的一个实例成员),他们只投中一外地来触发相同的行为,所以他们也很便宜,他们仍然可以经常反正冷落(例如,如果在该方法的第一个步骤涉及访问字段,因为它是)。

My question is relating to the performance characteristics of static methods vs instance methods and their scalability. Assume for this scenario that all class definitions are in a single assembly and that multiple discrete pointer types are required.

Consider:

public sealed class InstanceClass
{
      public int DoOperation1(string input)
      {
          // Some operation.
      }

      public int DoOperation2(string input)
      {
          // Some operation.
      }

      // … more instance methods.
}

public static class StaticClass
{
      public static int DoOperation1(string input)
      {
          // Some operation.
      }

      public static int DoOperation2(string input)
      {
          // Some operation.
      }

      // … more static methods.
}

The above classes represent a helper style pattern.

In an instance class, resolving the instance method take a moment to do as oppose to StaticClass.

My questions are:

  1. When keeping state is not a concern (no fields or properties are required), is it always better to use a static class?

  2. Where there is a considerable number of these static class definitions (say 100 for example, with a number of static methods each) will this affect execution performance or memory consumption negatively as compared with the same number of instance class definitions?

  3. When another method within the same instance class is called, does the instance resolution still occur? For example using the [this] keyword like this.DoOperation2("abc") from within DoOperation1 of the same instance.

解决方案

In theory, a static method should perform slightly better than an instance method, all other things being equal, because of the extra hidden this parameter.

In practice, this makes so little difference that it'll be hidden in the noise of various compiler decisions. (Hence two people could "prove" one better than the other with disagreeing results). Not least since the this is normally passed in a register and is often in that register to begin with.

This last point means that in theory, we should expect a static method that takes an object as a parameter and does something with it to be slightly less good than the equivalent as an instance on that same object. Again though, the difference is so slight that if you tried to measure it you'd probably end up measuring some other compiler decision. (Especially since the likelihood if that reference being in a register the whole time is quite high too).

The real performance differences will come down to whether you've artificially got objects in memory to do something that should naturally be static, or you're tangling up chains of object-passing in complicated ways to do what should naturally be instance.

Hence for number 1. When keeping state isn't a concern, it's always better to be static, because that's what static is for. It's not a performance concern, though there is an overall rule of playing nicely with compiler optimisations - it's more likely that someone went to the effort of optimising cases that come up with normal use than those which come up with strange use.

Number 2. Makes no difference. There's a certain amount of per-class cost for each member it terms of both how much metadata there is, how much code there is in the actual DLL or EXE file, and how much jitted code there'll be. This is the same whether it's instance or static.

With item 3, this is as this does. However note:

  1. The this parameter is passed in a particular register. When calling an instance method within the same class, it'll likely be in that register already (unless it was stashed and the register used for some reason) and hence there is no action required to set the this to what it needs to be set to. This applies to a certain extent to e.g. the first two parameters to the method being the first two parameters of a call it makes.

  2. Since it'll be clear that this isn't null, this may be used to optimise calls in some cases.

  3. Since it'll be clear that this isn't null, this may make inlined method calls more efficient again, as the code produced to fake the method call can omit some null-checks it might need anyway.

  4. That said, null checks are cheap!

It is worth noting that generic static methods acting on an object, rather than instance methods, can reduce some of the costs discussed at http://www.bluebytesoftware.com/blog/2011/10/23/OnGenericsAndSomeOfTheAssociatedOverheads.aspx in the case where that given static isn't called for a given type. As he puts it "As an aside, it turns out that extension methods are a great way to make generic abstractions more pay-for-play."

However, note that this relates only to the instantiation of other types used by the method, that don't otherwise exist. As such, it really doesn't apply to a lot of cases (some other instance method used that type, some other code somewhere else used that type).

Summary:

  1. Mostly the performance costs of instance vs static are below negligible.
  2. What costs there are will generally come where you abuse static for instance or vice-versa. If you don't make it part of your decision between static and instance, you are more likely to get the correct result.
  3. There are rare cases where static generic methods in another type result in fewer types being created, than instance generic methods, that can make it sometimes have a small benefit to turn rarely used (and "rarely" refers to which types it's used with in the lifetime of the application, not how often it's called). Once you get what he's talking about in that article you'll see that it's 100% irrelevant to most static-vs-instance decisions anyway. Edit: And it mostly only has that cost with ngen, not with jitted code.

Edit: A note on just how cheap null-checks are (which I claimed above). Most null-checks in .NET don't check for null at all, rather they continue what they were going to do with the assumption that it'll work, and if a access exception happens it gets turned into a NullReferenceException. As such, mostly when conceptually the C# code involves a null-check because it's accessing an instance member, the cost if it succeeds is actually zero. An exception would be some inlined calls, (because they want to behave as if they called an instance member) and they just hit a field to trigger the same behaviour, so they are also very cheap, and they can still often be left out anyway (e.g. if the first step in the method involved accessing a field as it was).

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

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