方法封装的性能开销 [英] Performance cost of Method Encapsulation
问题描述
有一个性能代价封装方法呢?一个非常简单的,武断的例子:
Is there a performance cost to encapsulating methods? A very brief, arbitrary example:
public static decimal Floor(decimal value)
{
return Math.Floor(value);
}
请问上面的函数被内联?如果是的话,那会是完全相同的从代码中调用Math.Floor()?我写这之前做了谷歌。
Would the above function be inlined? And if so, would it be the exact same as calling Math.Floor() from the code? I did Google before writing this.
推荐答案
方法可能会被内联(在JIT时,C#编译器的不联方法)。即使没有成本应该不会影响你的整体方案。由于优化和性能数据是针对特定的代码/应用程序,您需要,如果你看到性能问题来衡量你的情况
Method likely will be inlined (at JIT time, C# compiler does not inline method in IL). Even if not cost is unlikely to impact your overall program. Since optimization and performance numbers are specific to particular code/application you need to measure your case if you see performance problem.
在特定的编写更快的托管代码:知道什么MSDN上的东西成本的文章给出了如下估算方法调用的成本:最大6.8纳秒的(2003级机)如果呼叫未优化。
In particular Writing Faster Managed Code: Know What Things Cost article on MSDN gives following estimate for cost of method call: max 6.8 nano-seconds (for 2003 level machine) if the call is not optimized.
考虑阅读文章的其余部分。尤其是表的不仅是方法调用的成本,又能有多少3的会谈做运营的微不足道加法,减法,乘法和除法的成本。的
Consider reading the rest of the article. In particular Table 3 talks about not only the cost of method calls, but also how much do the operations as trivial as addition, subtraction, multiplication and division cost.
如果您需要确认方法是否被内联 - 它是覆盖在很多所以喜欢的我看看C#编译器内联方法调用?
If you need to confirm whether method is inlined - it is covered in many SO questions like Can I check if the C# compiler inlined a method call?
这篇关于方法封装的性能开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!