使用 .NET 内置类是否会带来显着的性能提升?
快速的小问题...
我知道有时在其他语言中,出于性能原因,库的部分代码是用特定于平台的直接 C 编写的。在这种情况下,您可以通过尽可能使用库代码来获得巨大性能提升。
那么.NET 平台是否这样做呢? Microsoft 的基类库实现是否以某种我不希望在托管代码中匹配的方式进行了优化?
类似于使用 KeyValuePair 作为类型安全的元组结构而不是编写我自己的东西怎么样?
Quick little question...
I know that sometimes in other languages libraries have part of their code written in platform-specific straight C for performance reasons. In such cases you can get huge performance gains by using library code wherever possible.
So does the .NET platform do this? Is Microsoft's implementation of the Base Class Library optimized in some way that I can't hope to match in managed code?
What about something little like using KeyValuePair as a type-safe tuple struct instead of writing my own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
据我所知,.NET Framework 的编译方式尚未创建一些无法访问的硬件加速或类似内容的挂钩,因此对于诸如 KeyValuePair 和
之类的简单内容Tuple
,你可能可以安全地推出自己的。然而,使用标准框架类还有许多其他优点,如果没有充分的理由,我会犹豫是否要编写自己的类。
更新
@gordy也提出了一个很好的观点,即每个人和他们的狗都在使用标准框架类,因此仅仅由于以下事实,就会有轻微的性能增益:
As far as I know, the .NET Framework hasn't been compiled in a way that creates hooks into some otherwise-inaccessible hardware acceleration or something like that, so for simple things like
KeyValuePair
andTuple
, you're probably safe rolling your own.However, there are a number of other advantages to using standard framework classes, and I'd hesitate to write my own without a strong reason.
Update
@gordy also makes a good point, that the standard framework classes are being used by everybody and their dog, so there will be a slight performance gain simply due to the fact that:
我自己也想知道这一点,但我怀疑情况并非如此,因为您可以“反编译”Reflector 中的所有基础库。
与自制的东西相比,可能仍然具有性能优势,因为代码可能已经被抖动并缓存了。
I've wondered this myself but I suspect that it's not the case since you can "decompile" all of base libraries in Reflector.
There's probably still a performance advantage over homemade stuff in that the code is likely jitted already and cached.
我建议您大部分时间都使用内置类,除非您已经测量过它不够快。
我很确定微软投入了大量的时间和精力来构建快速可靠的东西。经过几周的努力,你完全有可能击败他们。我只是认为大多数时候不值得花时间。
唯一一次似乎可以重写某些东西的时候是当它没有达到你想要的效果时。只需要注意时间成本和相关的难度即可。
I suggest you use built-in classes most of the time, UNLESS YOU'VE MEASURED IT'S NOT FAST ENOUGH.
I'm pretty sure MS put a lot of time and effort building something fast and reliable. It is totally possible you can beat them... after a few weeks of efforts. I just don't think it is worth the time most of the time.
The only time it seems ok to rewrite something is when it does not do all that you want. Just be aware of the time cost and the associated difficulty.
你能希望能达到匹配的表现吗?可能的是,尽管请记住他们的代码已经过全面测试并且经过了极其优化,所以我想说这不是一个值得付出的努力,除非您有一个非常具体的需求,没有可以直接满足的 BCL 类型。
.NET 4.0 已经有了一个很好的
Tuple
实现。但在 .NET 的早期版本中,如果您需要比 KeyValuePair 更大的内容,则必须自己推出。Could you ever hope to match the performance? Possibly, though keep in mind their code has been fully tested and extremely optimized, so I'd say it's not a worth-while effort unless you have a very specific need that there isn't a BCL type that directly fulfills.
And .NET 4.0 already has a good
Tuple<>
implementation. Though in previous versions of .NET you'd have to roll your own if you need anything bigger than a KeyValuePair.真正的性能提升来自于 MS 团队构建和测试库方法的事实。您可以非常放心,对象的行为不会引入错误。
然后就是重新发明轮子的问题。你这样做确实必须有一个充分的理由。
The real performance gain comes from the fact that the MS team built and tested the library methods. You can rest assured with a very high degree of comfort that the objects will behave without introducing bugs.
Then there is the matter of re-inventing the wheel. You'd really have to have a great reason for doing so.
主要的性能原因总是在于架构或复杂的算法,语言无关紧要。
Miscrosoft 基类库总是附带“重”方法的复杂性解释。因此您可以轻松决定使用它,或者找到另一个“更快”的算法来实现或使用。
当然,当涉及到繁重的算法(图形、归档等)时,使用较低级别语言所带来的性能提升就会派上用场。
Main performance reasons always lay in architecture or complex algorithms, language is no matter.
Miscrosoft Base Class Library always comes with a complexity explanation for "heavy" methods. So you can easily decide use it, or find another "faster" algorithm to implement or use.
Of corse when it comes to heavy algorithms (graphics, archiving, etc.) then performance gains from going to lower level language come in handy.