- Ngen.exe
- MultiCore JIT
- ReadyToRun
以下技术是针对
DevExpress技术交流群7:674691612 欢迎一起进群讨论
1. Ngen.exe
Native Image Generator (Ngen.exe) (原生图像生成器)是优化 .NET框架应用程序((.NET/. NET Core项目不支持Ngen)应该考虑的第一个工具,.NET框架项目使用Microsoft中间语言(MSIL)代码生成程序集。在执行应用程序之前,需要将此代码转换为机器代码,从MSIL到机器代码的转换在启动时就开始了——这个过程可能需要大量的时间。
开发人员可以使用Ngen.exe生成已经包含本机代码的原生映像库,需要注意的是,Ngen.exe应该在将要使用应用程序的机器上使用。开发人员可以在自己的机器上运行它来测试性能,但为了优化终端用户的冷启动,需要在用户机器上使用Ngen.exe。
在用户的机器上运行Ngen的最佳方法是将Ngen.exe合并到应用程序安装程序中。在安装过程中,开发人员需要执行以下命令行,Ngen.exe将自动处理与项目相关的所有程序集:
C:WindowsMicrosoft.NETFrameworkv4.0.30319ngen.exe install C:MyApp.exe
如果开发者将应用程序作为单击一次或存档(并且没有安装程序)分发,则可以在应用程序启动期间从代码中调用Ngen.exe。要在第一次启动时只运行Ngen.exe,为可执行文件计算一个哈希值,并在后续启动时检查这个哈希值:
var savedHash = string.Empty;var assemblyLocation = Assembly.GetEntryAssembly().Location;// Specify a path to the file that stores your executable’s hash.// Create this file or load the saved hash if the file already exists:var hashPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "hash.txt");if (!File.Exists(hashPath)) {File.Create(hashPath);} else {savedHash = File.ReadAllText(hashPath);}// Obtain the hash for your executable.// Cancel the operation if the application does not have changes:var hash = string.Concat(SHA1.Create().ComputeHash(File.ReadAllBytes(assemblyLocation)).Select(x => x.ToString("x2")));if (hash.Equals(savedHash))return;// Obtain the path to ngen.exe:var dotNetRuntimePath = RuntimeEnvironment.GetRuntimeDirectory();var ngenPath = Path.Combine(dotNetRuntimePath, "ngen.exe");// Create a process that runs ngen.exe:var process = new Process {StartInfo = new ProcessStartInfo {FileName = ngenPath,// Pass the path to your executable:Arguments = $"install "{assemblyLocation}"" /nologo""声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!