简体   繁体   English

C#中的静态方法?

[英]Static methods in C#?

What is the performance concern with static method over non-static methods? 静态方法相对于非静态方法的性能问题是什么? I have read that Static methods are better in terms of performance but i want to know, how they are faster? 我已经读过静态方法在性能方面更好但我想知道它们如何更快? If a method is not using any instance member then our compiler should take care of it and treat it as static method. 如果方法没有使用任何实例成员,那么我们的编译器应该处理它并将其视为静态方法。

Edit: Eric comments more on this here , and hints that there are some times when call is used... although note that his new() example isn't guaranteed ;-p 编辑:埃里克评论更多关于这个在这里 ,并暗示当一些次call时使用......但请注意他的new()例如不能保证 ;-p


In the original compiler (pre-1.1), the compiler did treat non-virtual instance methods (without this ) as static; 在原始编译器(1.1之前版本)中,编译器确实将非虚拟实例方法(没有this方法)视为静态; the problem was that this lead to some odd problems with null checking, ie 问题是这导致了一些奇怪的null检查问题,即

obj.SomeMethod();

didn't threw an exception (for obj=null and non-virtual method SomeMethod which didn't touch this ). 没有抛出异常(对于obj=null和非虚方法SomeMethod没有触及this )。 Which was bad if you ever changed the implementation of SomeMethod . 如果你改变了SomeMethod的实现,这很糟糕。 When they investigated the cost of adding the explicit null check (ie null-check then static-call), it turned out to be just the same as using a virtual-call, so they did that instead, which makes it far more flexible and predictable. 当他们调查添加显式空检查的成本(即null-check然后静态调用)时,结果与使用虚拟调用相同,所以他们这样做了,这使得它更灵活,可预测的。

Note that the "don't throw an exception" is also entirely the behaviour if SomeMethod is an extension-method (static). 请注意,如果SomeMethod是扩展方法(静态),则“不抛出异常”也完全是行为。

I think at one point you could emit IL to invoke a regular instance method via static-call, but the last time I tried I got the "oh no you don't!" 我想有一次你可以通过静态调用发出IL来调用常规实例方法,但是我最后一次尝试时得到了“哦,不,你没有!” message from the CLR (this operation may destabilise the runtime); 来自CLR的消息(此操作可能会破坏运行时的稳定性); either they blocked this entirely, or (perhaps more likely) I borked the custom IL. 要么他们完全阻止了这一点,要么(也许更有可能)我自定义IL。

Yes a static call would be faster - you don't need to create an instance of the object before you call the method. 是的,静态调用会更快 - 在调用方法之前,不需要创建对象的实例。 (Although you obviously won't notice the difference) (虽然你显然不会注意到差异)

In practical terms it doesn't matter if the compiler optimizes a method (makes the instance method static) - you won't call the instance method unless you've already created the instance already, right? 实际上,编译器是否优化方法(使实例方法为静态)并不重要 - 除非您已经创建了实例,否则不会调用实例方法,对吧?

At the end of the day you should rather try to optimize your code for maintainability rather than trying to save 3 nanoseconds here or there. 在一天结束时,您应该尝试优化代码以实现可维护性,而不是尝试在此处或那里节省3纳秒。

See this question . 看到这个问题

Here's the excerpt: 这是摘录:

a static call is 4 to 5 times faster than constructing an instance every time you call an instance method. 每次调用实例方法时,静态调用比构造实例快4到5倍。 However, we're still only talking about tens of nanoseconds per call 但是,我们仍然只说每次通话几十纳秒

I doubt the compiler will treat it as a static method, although you can check for yourself. 我怀疑编译器会将它视为静态方法,尽管您可以自己检查。 The benefit would be no creation of the instance. 好处是不会创建实例。 No garbage collector to worry about. 没有垃圾收集器担心。 And only the static constructor to be called, if there is one. 并且只有要调用的静态构造函数(如果有的话)。

static methods fast,because constructing an instance 静态方法很快,因为构造一个实例

buy if you only create a instance and save static member , performance is equal 如果你只创建一个实例并保存静态成员,那么性能是相等的

they are very small in total performance 它们的总体性能非常小

so ....... 所以.......

yes static method is fast but the memory acquired by the static variable is not controlled by GC and is not released even if it is not needed, so that is an issue. 是静态方法很快但静态变量获取的内存不受GC控制,即使不需要也不会释放,因此这是一个问题。

but more than anything else you should consider the design of the allpication as the memory and speed has increased by days but your design may suck if you dont make use of static variables properly. 但最重要的是你应该考虑因为内存和速度增加了几天所以你的设计可能很糟糕,如果你没有正确使用静态变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM