可以为 .NET 应用程序关闭 JIT 编译吗?
.NET 使用 JIT(即时)编译技术。这意味着它首先将源代码编译为 MSIL(中间语言)并创建一个 EXE,然后在运行时将该 IL 转换为机器代码。它工作正常,但是整个编译的代码没有加载到缓存中,并且它一次又一次地编译所有函数和过程。我的申请对时间非常敏感。发生的情况是,当我非常频繁地调用某个特定函数(例如通过 do while 循环)时,该函数会在 10 微秒内执行。但同样的函数,如果间隔100毫秒甚至更长的时间调用,则执行时间需要50-60微秒。我希望一些特定的函数/过程始终保留在缓存中。有人可以指导吗?我认为这是由于 JIT 造成的。我们可以关闭 JIT 并将代码直接编译成机器语言之类的吗?请帮忙。
.NET uses the JIT (just-in-time) compilation technique. It means it first compiles the source code into MSIL (Intermediate language) and creates an EXE and later transforms that IL into machine code at runtime. It works fine, but the whole compiled code is not loaded into cache and it compiles all the functions and procedures again and again. My application is very time sensitive. What is happening is that when I call up a particular function very frequently, say through a do while loop, the function gets executed within 10 microseconds. But the same function, if called after an interval of 100 milliseconds or more, it takes 50-60 microseconds to execute. I want some particular functions/procedures to remain in cache all the time. Can anybody guide? I think this is happening because of JIT. Can we turn off JIT and compile the code straight into machine language or something? Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦函数第一次被 JITted,在进程运行时它就永远不会被忘记。由于 IL 到本机编译,这是唯一可能需要更长时间的第一次调用。最坏的情况是,如果系统 RAM 不足,本机代码可能会被换出。
您可以通过运行
ngen
并将预编译的本机映像放入 GAC 来跳过 JIT 编译,尽管它不会解决您的问题。如果您的代码对时间要求非常严格,需要 50-60 usec 才能产生影响,那么您首先就不应该使用 .NET - 不是因为 JIT,而是因为 GC,它实际上可能由任何托管线程随时触发。Once function is JITted for the first time, it's never forgotten while the process runs. It's the only first call that might take longer due to IL-to-native compilation. Worst case scenario is that native code might be swapped out if system is short on RAM.
You can skip JIT compilation by running
ngen
and placing precompiled native image in GAC, although it won't be the solution to your problems. If your code is so time-critical that 50-60 usec makes the difference, you shouldn't use .NET in the first place - not due to JIT, but because of GC which might be triggered virtually at any time by any managed thread.不,您无法关闭 JIT,因为 .NET 不会“解释”IL。它必须被编译为机器代码,这是由 JITter 完成的。
你可以做的就是预编译它。这是通过 ngen 完成的。请参阅 http://msdn.microsoft.com/en-us/magazine/cc163610。 ASPX
No, you can't turn off JIT, because .NET does not "interpret" the IL. It has to be compiled to machine code, and that's done by the JITter.
What you CAN do is pre-compile it. This is done with ngen. see http://msdn.microsoft.com/en-us/magazine/cc163610.aspx