简体   繁体   English

在C#类中,私有,受保护,公共和内部方法的性能是否有所不同?

[英]Is there any difference regarding performance of private, protected, public and internal methods in C# classes?

Is there any difference regarding performance of private , protected , public and internal methods in C# class? 在C#类中, privateprotectedpublicinternal方法的性能是否有所不同? I'm interested if one consumes more processor time or RAM. 我很想知道是否要消耗更多的处理器时间或RAM。

I'm not aware of any performance difference for normal invocation; 我不知道常规调用的任何性能差异; it's possible that more restricted access will take a little more work when accessing via dynamic invocation or reflection as the caller may need to be validated more carefully. 通过动态调用或反射进行访问时,受限制的访问可能会花费更多的工作,因为可能需要更仔细地验证调用方。 In the normal JIT-compiled case the access can be validated by the CLR just once and then taken for granted. 在普通的JIT编译情况下,CLR可以仅对访问进行一次验证,然后将其视为理所当然。 I guess it's possible that the JIT compilation (and IL verification) itself could be slightly slower for more restrictive access - but I find it hard to believe it would be significant. 我想对于更严格的访问,JIT编译(和IL验证)本身可能会稍慢一些-但我发现很难相信它会很重要。

This should absolutely not be a factor in determining which accessibility to use, even if somehow there is some tiny performance difference I'm unaware of. 绝对不是决定使用哪种可访问性的因素,即使我不知道有多少微小的性能差异。 If you believe you may be able to achieve a performance benefit by making the accessibility something other than the "natural" one for your design, you should definitely benchmark the before/after case - I suspect you'll be hard-pressed to find a real-world situation where the difference is reliably measurable. 如果您认为通过为设计提供非“自然”的可访问性可能能够获得性能上的好处,那么您绝对应该对之前/之后的情况进行基准测试-我怀疑您很难找到一个可以可靠地测量差异的真实情况。

The same sort of advice goes for all kinds of micro-optimization: it's almost never a good idea anyway, and should definitely only be undertaken within careful measuring. 各种各样的微观优化都使用相同的建议:无论如何这几乎不是一个好主意, 绝对应该在仔细测量的情况下进行。

There will be no measurable difference in performance between private, protected or public methods. 私有,受保护或公共方法之间的性能没有可测量的差异。

If you focus on optimization, possibly you should try making your bottleneck piece of code more "procedural" than object-oriented. 如果您专注于优化,则可能应该尝试使瓶颈代码比面向对象的代码更具“过程性”。 It would do small improvement. 它将做一些小的改进。

暂无
暂无

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

相关问题 c#不一致的可访问性阻止将私有类作为受保护方法的参数 - c# Inconsistent Accessibility Prevents Private Classes as parameters for Protected Methods C#:在密封类中模拟和测试受保护(或私有)方法 - 方法 - C#: Mocking and testing protected (or private) methods in sealed classes — approaches 对C#中的公共,静态和私有方法/类/变量感到困惑 - Confused about public, static and private methods/classes/variables in C# 有关其在C#(和其他语言)中的内存占用的私有方法和属性与公共方法和属性 - Private methods and properties vs public ones regarding their memory footprints in C# (and other languages) 在C#中,public、private、protected和没有访问修饰符有什么区别? - In C#, what is the difference between public, private, protected, and having no access modifier? 在C#中受保护的访问说明符和受保护的内部有什么区别 - What is the difference between access specifier protected and internal protected in C# C#,使公共成员的方法变得私密 - C#, making public members their methods private 如何在运行时查找使用C#加载/执行的私有/公共类,属性和方法 - How to find out the private/public classes, properties, and methods being loaded/executed with C# during runtime 如何确定方法是私有的,受保护的,内部的还是公共的? - How to decide if a method will be private, protected, internal or public? “私人”和“受保护的内部”有什么区别? - What is the difference between “private” and “protected Internal”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM