简体   繁体   English

混合模式.NET程序集有更好的性能吗?

[英]Do mixed-mode .NET Assembly have better performance?

I am very new to .Net programming, roughly 1 year. 我是.Net编程的新手,大约1年。

few days back i checked few articles related to mixed mode assembly, out of curiosity, i am having trouble to understand a lot of this. 几天前我查了几篇与混合模式装配有关的文章,出于好奇,我很难理解很多这个。

  1. Why Microsoft introduced Mixed-Mode assembly, if P/Invoke can be easily done with C#/VB.NET? 为什么Microsoft引入混合模式程序集,如果可以使用C#/ VB.NET轻松完成P / Invoke?

  2. Does the Mixed-mode assembly execute faster than pure .Net assembly? 混合模式程序集的执行速度是否比纯.Net程序集快?

Since .Net is CLR (much like a VM), so my best guess is assembly compiled under mixed mode will too be in CLR so that will eventually degrade the performance. 由于.Net是CLR(很像VM),所以我最好的猜测是在混合模式下编译的汇编也会出现在CLR中,这样最终会降低性能。

Please help me with my confusions! 请帮我解决一下我的困惑!

The primary use case for mixed-mode assemblies is having to interop with existing C or C++ code. 混合模式程序集的主要用例是必须与现有的 C或C ++代码互操作。 Very common of course. 当然很常见。

It doesn't by design make code faster. 它不是设计使代码更快。 There's a cost associated with transitioning from managed code to native code execution. 从托管代码转换到本机代码执行会产生相关成本。 It is heavily optimized, if you do it right then you only pay for the cost of writing a cookie on the stack that the garbage collector recognizes so that it will avoid looking through unmanaged stack frames for object references. 它经过了大量优化,如果您这样做,那么您只需支付在垃圾收集器识别的堆栈上编写cookie的成本,这样就可以避免查看非托管堆栈帧以进行对象引用。 Costs but a handful of cpu cycles. 成本但少数cpu周期。 But additional cost may be incurred when you need to translate data. 但是,当您需要翻译数据时可能会产生额外费用。 A string being the canonical example. 字符串是规范的例子。 Or pin a managed object so it can't be moved by the collector, an array being the canonical example. 或者固定托管对象,使其不能被收集器移动,数组是规范示例。

But yes, you certainly can use mixed-mode assemblies to solve a perf problem. 但是,您当然可以使用混合模式程序集来解决性能问题。 If you can limit the number of times you make a transition and you can take advantage of the C or C++ compiler's ability to generate highly optimized code then you could be ahead. 如果您可以限制转换的次数,并且您可以利用C或C ++编译器生成高度优化的代码的能力,那么您可以领先。 There's no magic formula that says you will and it certainly isn't that easy to beat the jitter. 没有任何神奇的公式可以表明你的意愿,它肯定不容易击败抖动。 It has a pretty effective optimizer although it can't quite beat a C compiler's optimizer, given that it operates under time constraints. 它有一个非常有效的优化器,虽然它不能完全击败C编译器的优化器,因为它在时间限制下运行。 The quality of the code you write matters the most. 您编写的代码质量最重要。

Always use a profiler first to eliminate any bloopers, find the actual hot spots and to ensure it is worth trying. 始终首先使用分析器消除任何花絮,找到实际的热点并确保它值得尝试。

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

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