问题描述
我遇到过很多优化技巧,其中说您应该将类标记为密封的以获得额外的性能优势.
I have come across a lot of optimization tips which say that you should mark your classes as sealed to get extra performance benefits.
我运行了一些测试来检查性能差异,但没有发现.难道我做错了什么?我是否错过了密封类会产生更好结果的情况?
I ran some tests to check the performance differential and found none. Am I doing something wrong? Am I missing the case where sealed classes will give better results?
有没有人运行过测试并看到不同之处?
Has anyone run tests and seen a difference?
帮助我学习:)
推荐答案
JITter 有时会使用对密封类中方法的非虚拟调用,因为它们无法进一步扩展.
The JITter will sometimes use non-virtual calls to methods in sealed classes since there is no way they can be extended further.
关于调用类型、虚拟/非虚拟,有一些复杂的规则,我不知道所有这些,所以我不能真正为你概述它们,但是如果你用谷歌搜索密封类和虚拟方法,你可能会找到一些关于主题.
There are complex rules regarding calling type, virtual/nonvirtual, and I don't know them all so I can't really outline them for you, but if you google for sealed classes and virtual methods you might find some articles on the topic.
请注意,您从这种优化级别获得的任何类型的性能优势都应被视为最后的手段,始终在算法级别进行优化,然后再在代码级别进行优化.
Note that any kind of performance benefit you would obtain from this level of optimization should be regarded as last-resort, always optimize on the algorithmic level before you optimize on the code-level.
这是一个提到这一点的链接:在密封关键字上闲逛
Here's one link mentioning this: Rambling on the sealed keyword
这篇关于密封类真的能提供性能优势吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!