使方法静态可以提高性能,在什么情况下?
何时(如果有的话)将参数作为参数传递给静态方法而不是让该方法成为非静态并通过实例成员访问相同的值更快。假设该方法以只读方式访问这些成员。
在所有其他条件相同的情况下,调用静态方法稍快而不是调用实例方法。
在所有其他条件相同的情况下,调用不带参数的方法比调用带参数的方法稍快。
考虑一下:
private Thing _thing;
void DoTheThing()
{
_thing.DoIt();
}
与这个等效的代码相比:
private Thing _thing;
// caller's responsibility to pass "_thing"
static void DoTheThing(Thing thing)
{
thing.DoIt();
}
我无法想象这种优化会真正增加任何价值的现实情况,但作为一个思想实验(对于那些喜欢讨论这种事情的人),真的存在吗?一个好处,如果是这样,那么有多少参数(什么类型等)使平衡向相反的方向倾斜?
是否还有其他因素会影响这一点?例如,静态方法将 _thing
作为局部变量而不是字段进行访问。
When, if ever, is it faster to pass arguments as arguments to a static method rather than have the method be non-static and access the same values via instance members. Assume the method accesses these members in a read-only fashion.
All other things being equal, calling a static method is slightly faster than calling an instance method.
All other things being equal, calling a method with no arguments is slightly faster than calling one with arguments.
Consider:
private Thing _thing;
void DoTheThing()
{
_thing.DoIt();
}
Versus this equivalent code:
private Thing _thing;
// caller's responsibility to pass "_thing"
static void DoTheThing(Thing thing)
{
thing.DoIt();
}
I can't think of a real-world situation where this kind of optimisation would really add any value, but as a thought experiment (for those who like to discuss this kind of thing), is there really a benefit, and if so then how many arguments (of what types etc) tip the balance the other way?
Would any other factors play into the consideration of this? The static method accesses _thing
as a local variable rather than a field, for example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以想到一个可能的性能优势(对于非虚拟方法):静态方法不需要首先测试引用是否为空(抛出
NullReferenceException 在适当的情况下)。
我认为目前这不会带来任何优势,但这是一种可能的优势。不过,我不确定它是否适用于您的特定示例 - 并且很难看出它如何适用于您实际上想要使用该值的任何情况。
There's one possible performance benefit I can thnk of (for a non-virtual method): the static method doesn't need to test a reference for nullity first (to throw a
NullReferenceException
where appropriate).I don't think this currently gives any advantage, but it's a possible one. I'm not sure it would apply in your particular example, though - and it's hard to see how it would apply in any case where you actually wanted to use the value.
在您的情况下(我假设代码示例位于 Thing 类中)静态和非静态根本没有速度差异。这是来自您的链接:
因此,将其静态化以提高速度是没有任何意义的。
另请注意,链接页面中提供的值来自 .Net 1.1 并且已经过时。
In your case (I'm assuming the code sample would be in the Thing class) static and non-static will have no speed difference at all. This is from YOUR link:
So there is no sense at all in making it static for a speed boost.
Also take into account that the values provided in your linked page are from .Net 1.1 and way outdated.
我不确定静态方法和实例方法之间的性能统计。
但我认为应该根据对象设计来决定将其作为静态方法还是实例方法。如果通过调用您的方法,对象的状态没有改变,那么您应该将该方法设置为静态方法(针对该类型的方法,而不是针对该类型的特定实例的方法)。
I am not sure about the performance statics among static methods and instance methods.
But I believe that the decision should be taken whether you make it as a static method or instance method on the basis of object design. If by calling your method, the state of the object is not altered, then you should make that method as static method (method for the type, not for a perticular instance of the type).