我可以使用一个简洁的代码片段来进行JIT内联吗? [英] Can I have a concise code snippet that would incur JIT inlining please?
问题描述
我正在尝试生成一些"Hello World"大小的C#代码段,这些代码段会导致JIT内联.到目前为止,我有这个:
I'm trying to produce some "Hello World" size C# code snippet that would incur JIT inlining. So far I have this:
class Program
{
static void Main(string[] args)
{
Console.WriteLine( GetAssembly().FullName );
Console.ReadLine();
}
static Assembly GetAssembly()
{
return System.Reflection.Assembly.GetCallingAssembly();
}
}
我在Visual Studio中将
编译为发布"-任何CPU"和无需调试即可运行".它显示了我的示例程序程序集的名称,因此很明显GetAssembly()
没有内联到Main()
中,否则它将显示mscorlib
程序集名称.
which I compile as "Release"-"Any CPU" and "Run without debugging" from Visual Studio. It displays the name of my sample program assembly so clearly GetAssembly()
is not inlined into Main()
, otherwise it would display mscorlib
assembly name.
如何编写一些会导致JIT内联的C#代码段?
How do I compose some C# code snippet that would incur JIT inlining?
推荐答案
当然,这是一个示例:
using System;
class Test
{
static void Main()
{
CallThrow();
}
static void CallThrow()
{
Throw();
}
static void Throw()
{
// Add a condition to try to disuade the JIT
// compiler from inlining *this* method. Could
// do this with attributes...
if (DateTime.Today.Year > 1000)
{
throw new Exception();
}
}
}
以类似发行版的方式进行编译:
Compile in a release-like mode:
csc /o+ /debug- Test.cs
运行:
c:\Users\Jon\Test>test
Unhandled Exception: System.Exception: Exception of type 'System.Exception' was
thrown.
at Test.Throw()
at Test.Main()
请注意堆栈跟踪-看起来Throw
是直接由Main
调用的,因为CallThrow
的代码是内联的.
Note the stack trace - it looks as if Throw
was called directly by Main
, because the code for CallThrow
was inlined.
这篇关于我可以使用一个简洁的代码片段来进行JIT内联吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!