有没有办法在 C# 中强制执行函数内联?
据我所知,没有办法暗示 c# 编译器内联特定函数,我猜设计就是这样。
我还认为,不让程序员指定内联什么和不内联什么通常是一个好主意,因为这意味着您认为自己比 JIT 编译器更聪明(我对那些实际上是这样的人表示敬意),但是,什么如果我想指定代码的关键部分需要不惜一切代价非常快,无论如何在目标机器上实现它? 到目前为止你还不能做这样的事情,我想知道 C# 语言和 JIT 是否都支持这个功能。
就我而言,我知道目标机器是什么,并且我知道函数内联将有助于提高性能。 这让我认为强制执行函数内联的唯一方法是了解 JIT 在什么情况下会执行此操作,但我认为这也不是一个好主意,
任何有关该主题的说明都将不胜感激。
谢谢。
As far as I know there's no way to hint the c# compiler to inline a particular function and I guess it's like that by design.
I also think that not letting the programmer to specify what to inline and what not is generally a good idea, as it would imply that you think you're smarter than the JIT compiler (my respects to those who actually are), but, what if I wanted to specify that a critical portion of code needs to be extremely fast at any cost, no matter how to achieve it on the target machine? As of yet you can't do such a thing and I wonder if both the c# language and the JIT will ever support this feature.
In my case, I know what the target machine is, and I know that function inlining will help improve the performance. This leaves me thinking that the only way to enforce function inlining is getting to know under what circumstances the JIT will do it but I don't think that's a good idea either,
Any light on the subject would be much appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简短回答:否
长回答: http://blogs.msdn .com/ericgu/archive/2004/01/29/64644.aspx
内联标准:http://blogs.msdn.com/davidnotario/archive/2004/11/01/250398.aspx 和 http://blogs.msdn.com/ericgu/archive/2004/01/29/64717.aspx
请注意,在最后两个关于内联标准的链接中,关于结构不是内联的链接已经过时了; 更新的信息可以在以下位置找到:http://blogs.msdn.com/vancem/archive/2008/05/12/what-s-coming-in-net-runtime-performance-in -版本-v3-5-sp1.aspx
Short answer: no
Long answer: http://blogs.msdn.com/ericgu/archive/2004/01/29/64644.aspx
Criteria for inlining: http://blogs.msdn.com/davidnotario/archive/2004/11/01/250398.aspx and http://blogs.msdn.com/ericgu/archive/2004/01/29/64717.aspx
Note that in the last two links about criteria for inlining, the one about structs not being inlines is out-of-date; updated information can be found at: http://blogs.msdn.com/vancem/archive/2008/05/12/what-s-coming-in-net-runtime-performance-in-version-v3-5-sp1.aspx
随着 .Net 4.5 的出现,情况发生了一些变化。
现在,您可以使用属性
[MethodImplAttribute(MethodImplOptions.AggressiveInlined)]
这将导致它在可能的情况下被 JIT 内联。请参阅 此博客了解更多详细信息。
The situation has changed a little with the advent of .Net 4.5.
You can now decorate a method with the attribute
[MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
which will cause it to be inlined by the JIT if at all possible.See this blog for more details.
我最近对此进行了基准测试:
http://www.gfilter.net/junk/BubblesortBenchmark.jpg (越高则越差)
正如您所知,CLR 和 JVM 在方法内联方面都比您以前做得更好。
I recently benchmarked this:
http://www.gfilter.net/junk/BubblesortBenchmark.jpg (Higher is worse)
As you can tell, the CLR and the JVM are both much better at method inlining than you ever will be.
如果您正在执行这条罕见的路径,请打破您的 C 或 ASM。
If you're executing that rare path, break out your C or ASM.
替代文本 http://web.ics.purdue.edu/~ssanty/图片/1.gif
alt text http://web.ics.purdue.edu/~ssanty/images/1.gif