如何加快第一次执行.NET应用程序的性能?

时间:2021-06-21 23:39:48

Our C# client applications always take a much longer time to load on their first run. I haven't gone so far as to test if it is the first run of any .NET app that is slower, or if the first run of each .NET app is slower, but it remains a problem in any case. How can we eliminate this one-time startup hit?

我们的C#客户端应用程序在首次运行时总是需要更长的时间才能加载。我还没有测试它是否是任何较慢的.NET应用程序的第一次运行,或者每个.NET应用程序的第一次运行速度较慢,但​​在任何情况下它仍然是一个问题。我们怎样才能消除这种一次性的创业打击?

My initial thoughts are that some sort of service could "warm up" the libraries. Would we need to do this for each of our apps, or just any .NET app? Would the user the service runs as make a difference? Perhaps rather than a Windows service, an application that runs on Windows login could do the dirty work? Again, would the fact that it's a .NET service be enough, or would we have to run each of our programs to eliminate the penalty? We could pass in a command-line parameter that would tell the program to exit immediately, but would that be sufficient, or would we need .NET to load each assembly we'll be using during the normal execution of the application?

我最初的想法是,某种服务可以“热身”库。我们需要为每个应用程序或任何.NET应用程序执行此操作吗?服务运行的用户是否会有所作为?也许不是Windows服务,在Windows登录上运行的应用程序可以执行肮脏的工作吗?那么,它是一个.NET服务是否足够,或者我们是否必须运行我们的每个程序以消除惩罚?我们可以传入一个命令行参数来告诉程序立即退出,但这是否足够,或者我们是否需要.NET来加载我们将在应用程序的正常执行期间使用的每个程序集?


Re: Some answers, we are deploying release-mode DLLs, and the slowdown is only on the first startup. We are delaying initialization of classes as much as possible.

Re:有些答案,我们正在部署发布模式的DLL,而减速仅在第一次启动时。我们正在尽可能地延迟类的初始化。

4 个解决方案

#1


Other answers have talked about JIT time, but in my experience another very significant factor is the time taken to load the .NET framework itself for the first time after a boot.

其他答案已经谈到了JIT时间,但根据我的经验,另一个非常重要的因素是在引导后第一次加载.NET框架本身所花费的时间。

Try writing an absolutely trivial program (although preferrably one which at least touches the assemblies your real code uses). Compile it and reboot. Run the trivial program, and then (when it's finished) run your real application - see how that compares with running your real application after a reboot without running the trivial application as well.

尝试编写一个绝对琐碎的程序(尽管最好是一个至少触及真实代码使用的程序集的程序)。编译并重新启动。运行简单的程序,然后(当它完成时)运行您的真实应用程序 - 看看如何与重新启动后运行您的真实应用程序相比,而不运行简单的应用程序。

#2


Its the JIT (Just In Time compilation) that lets you wait the first time you run your .net apps. This compiles your IL to machine code before running the code. The reason this happens only the first time is that the machine code version is then stored on disk an reused.

它是JIT(即时编译),可让您在第一次运行.net应用程序时等待。这会在运行代码之前将IL编译为机器代码。这种情况仅在第一次发生的原因是机器代码版本随后被存储在磁盘上并被重用。

You can use NGEN.EXE to pre-jit your application...

您可以使用NGEN.EXE预先应用您的应用程序...

This needs to be done on the computer you run the software on, since the JIT will compile and optimize for the CPU it is running on. You could do this as a part of your programs installation...

这需要在运行该软件的计算机上完成,因为JIT将对其运行的CPU进行编译和优化。您可以将此作为程序安装的一部分......

#3


Read this (Improving Application Startup Time), this (CLR Optimizations In .NET Framework 3.5 SP1) and this (The Performance Benefits of NGen).

阅读本文(改进应用程序启动时间),这(.NET Framework 3.5 SP1中的CLR优化)和此(NGen的性能优势)。

Main idea to improve startup time is to use delayed initialization whenever possible. Do not instantiate things that unnecessary immediately after startup. Etc, etc, etc.

改善启动时间的主要思想是尽可能使用延迟初始化。启动后不要立即实例化那些不必要的东西。等等等

#4


I have a feeling the startup delay is in the .NET runtime converting MSIL to native code.

我有一种感觉,启动延迟是在.NET运行时将MSIL转换为本机代码。

You could possibly ngen your assemblies upon installation, this would compile them to native code and would possibly prevent the startup delay. WiX (http://wix.sourceforge.net) has a custom action that does just that.

你可能会在安装时使用你的程序集,这会将它们编译为本机代码,并可能防止启动延迟。 WiX(http://wix.sourceforge.net)有一个自定义操作就可以做到这一点。

#1


Other answers have talked about JIT time, but in my experience another very significant factor is the time taken to load the .NET framework itself for the first time after a boot.

其他答案已经谈到了JIT时间,但根据我的经验,另一个非常重要的因素是在引导后第一次加载.NET框架本身所花费的时间。

Try writing an absolutely trivial program (although preferrably one which at least touches the assemblies your real code uses). Compile it and reboot. Run the trivial program, and then (when it's finished) run your real application - see how that compares with running your real application after a reboot without running the trivial application as well.

尝试编写一个绝对琐碎的程序(尽管最好是一个至少触及真实代码使用的程序集的程序)。编译并重新启动。运行简单的程序,然后(当它完成时)运行您的真实应用程序 - 看看如何与重新启动后运行您的真实应用程序相比,而不运行简单的应用程序。

#2


Its the JIT (Just In Time compilation) that lets you wait the first time you run your .net apps. This compiles your IL to machine code before running the code. The reason this happens only the first time is that the machine code version is then stored on disk an reused.

它是JIT(即时编译),可让您在第一次运行.net应用程序时等待。这会在运行代码之前将IL编译为机器代码。这种情况仅在第一次发生的原因是机器代码版本随后被存储在磁盘上并被重用。

You can use NGEN.EXE to pre-jit your application...

您可以使用NGEN.EXE预先应用您的应用程序...

This needs to be done on the computer you run the software on, since the JIT will compile and optimize for the CPU it is running on. You could do this as a part of your programs installation...

这需要在运行该软件的计算机上完成,因为JIT将对其运行的CPU进行编译和优化。您可以将此作为程序安装的一部分......

#3


Read this (Improving Application Startup Time), this (CLR Optimizations In .NET Framework 3.5 SP1) and this (The Performance Benefits of NGen).

阅读本文(改进应用程序启动时间),这(.NET Framework 3.5 SP1中的CLR优化)和此(NGen的性能优势)。

Main idea to improve startup time is to use delayed initialization whenever possible. Do not instantiate things that unnecessary immediately after startup. Etc, etc, etc.

改善启动时间的主要思想是尽可能使用延迟初始化。启动后不要立即实例化那些不必要的东西。等等等

#4


I have a feeling the startup delay is in the .NET runtime converting MSIL to native code.

我有一种感觉,启动延迟是在.NET运行时将MSIL转换为本机代码。

You could possibly ngen your assemblies upon installation, this would compile them to native code and would possibly prevent the startup delay. WiX (http://wix.sourceforge.net) has a custom action that does just that.

你可能会在安装时使用你的程序集,这会将它们编译为本机代码,并可能防止启动延迟。 WiX(http://wix.sourceforge.net)有一个自定义操作就可以做到这一点。