如何提高delphi应用程序的启动速度?

时间:2022-03-14 15:25:04

What do you do to increase startup speed (or to decrease startup time) of your Delphi app?

您如何提高Delphi应用程序的启动速度(或减少启动时间)?

Other than application specific, is there a standard trick that always works?

除了特定于应用程序,是否有一个标准的技巧总是有效?

Note: I'm not talking about fast algorithms or the likes. Only the performance increase at startup, in terms of speed.

注意:我不是在谈论快速算法等。在速度方面,只有启动时的性能提升。

11 个解决方案

#1


Try doing as little as possible in your main form's OnCreate event. Rather move some initialization to a different method and do it once the form is shown to the user. An indicator that the app is busy with a busy mouse cursor goes a long way.

尝试在主窗体的OnCreate事件中尽可能少地做。而是将一些初始化移动到不同的方法,并在向用户显示表单后执行此操作。应用程序忙于忙碌鼠标光标的指示器很长。

Experiments done shows that if you take the exact same application and simply add a startup notification to it, users actually perceive that app as starting up faster!

完成的实验表明,如果您使用完全相同的应用程序并只是向其添加启动通知,用户实际上会认为该应用程序启动速度更快!

Other than that you can do the usual things like exclude debug information and enable optimization in the compiler.

除此之外,您可以执行常规操作,例如排除调试信息并在编译器中启用优化。

On top of that, don't auto create all your forms. Create them dynamically as you need them.

最重要的是,不要自动创建所有表单。根据需要动态创建它们。

#2


In the project options, don't auto-create all of your forms up front. Create and free them as needed.

在项目选项中,不要预先自动创建所有表单。根据需要创建并释放它们。

#3


Well, as Argalatyr suggested I change my comment to a separate answer:

好吧,正如Argalatyr建议我将我的评论改为单独的答案:

As an extension to the "don't auto create forms" answer (which will be quite effective by itself) I suggest to delay opening connections to databases, internet, COM servers and any peripheral device until you need it first.

作为“不自动创建表单”答案的扩展(这本身就非常有效)我建议延迟打开与数据库,互联网,COM服务器和任何外围设备的连接,直到您首先需要它为止。

#4


Three things happen before your form is shown:

在您的表单显示之前会发生三件事:

  1. All 'initialization' blocks in all units are executed in "first seen" order.
  2. 所有单元中的所有“初始化”块都以“第一次看到”的顺序执行。

  3. All auto-created forms are created (loaded from DFM files and their OnCreate handler is called)
  4. 创建所有自动创建的表单(从DFM文件加载并调用其OnCreate处理程序)

  5. You main form is displayed (OnShow and OnActivate are called).
  6. 显示主窗体(调用OnShow和OnActivate)。

As other have pointed out, you should auto-create only small number of forms (especially if they are complicated forms with lots of component) and should not put lengthy processing in OnCreate events of those forms. If, by chance, your main form is very complicated, you should redesign it. One possibility is to split main form into multiple frames which are loaded on demand.

正如其他人所指出的,你应该只自动创建少量的表单(特别是如果它们是具有大量组件的复杂表单),并且不应该在这些表单的OnCreate事件中进行冗长的处理。如果您的主要形式非常复杂,您应该重新设计它。一种可能性是将主表单拆分为多个按需加载的帧。

It's also possible that one of the initialization blocks is taking some time to execute. To verify, put a breakpoint on the first line of your program (main 'begin..end' block in the .dpr file) and start the program. All initialization block will be executed and then the breakpoint will stop the execution.

其中一个初始化块也可能需要一些时间来执行。要验证,请在程序的第一行放置断点(.dpr文件中的主“begin..end”块)并启动程序。将执行所有初始化块,然后断点将停止执行。

In a similar way you can step (F8) over the main program - you'll see how long it takes for each auto-created form to be created.

以类似的方式,您可以在主程序上执行步骤(F8) - 您将看到创建每个自动创建的表单需要多长时间。

#5


Display a splash screen, so people won't notice the long startup times :).

显示启动画面,这样人们就不会注意到启动时间长:)。

#6


Fastest code - it's the code, that never runs. Quite obvious, really ;)

最快的代码 - 它是代码,永远不会运行。很明显,真的;)

#7


Deployment of the application can (and usually does!) happen in ways the developer may not have considered. In my experience this generates more performance issues than anyone would want.

应用程序的部署可以(通常会!)以开发人员可能没有考虑过的方式进行。根据我的经验,这会产生比任何人想要的更多性能问题。

A common bottleneck is file access - a configuration file, ini file that is required to launch the application can perform well on a developer machine, but perform abysmally in different deployment situations. Similarly, application logging can impede performance - whether for file access reasons or log file growth.

常见的瓶颈是文件访问 - 启动应用程序所需的配置文件,ini文件可以在开发人员计算机上很好地执行,但在不同的部署情况下执行得非常糟糕。同样,应用程序日志记录会阻碍性能 - 无论是出于文件访问原因还是日志文件增长。

What I see so often are rich-client applications deployed in a Citrix environment, or on a shared network drive, where the infrastructure team decides that user temporary files or personal files be stored in a location that the application finds issues with, and this leads to performance or stability issues.

我经常看到的是在Citrix环境或共享网络驱动器上部署的富客户端应用程序,基础架构团队决定将用户临时文件或个人文件存储在应用程序发现问题的位置,这导致性能或稳定性问题。

Another issue I often see affecting application performance is the method used to import and export data to files. Commonly in Delphi business applications I see export functions that work off DataSets - iterating and writing to file. Consider the method used to write to file, consider memory available, consider that the 'folder' being written to/read from may be local to the machine, or it may be on a remote server.

我经常看到影响应用程序性能的另一个问题是用于将数据导入和导出到文件的方法。通常在Delphi业务应用程序中,我看到可以使用DataSet的导出函数 - 迭代和写入文件。考虑用于写入文件的方法,考虑可用的内存,考虑写入/读取的“文件夹”可以是机器本地的,也可以是远程服务器上的。

A developer may argue that these are installation issues, outside the scope of their concern. I usually see many cycles of developer analysis on this sort of issue before it is identified as an 'infrastructure issue'.

开发人员可能会认为这些是安装问题,超出了他们关注的范围。在将此类问题确定为“基础架构问题”之前,我通常会看到许多开发人员分析周期。

#8


  • First thing to do is to clear auto created forms list (look for Project Options). Create forms on the fly when needed, especially if the application uses database connection (datamodule) or forms that include heavy use of controls.
  • 首先要做的是清除自动创建的表单列表(查找项目选项)。在需要时动态创建表单,尤其是在应用程序使用数据库连接(数据模块)或包含大量控件的表单时。

  • Consider using form inheritance also to decrease exe size (resource usage is mimized)
  • 考虑使用表单继承也减少exe大小(资源使用被模仿)

  • Decrease number of forms and merge similar or related functionality into single form
  • 减少表单数量并将类似或相关的功能合并为单个表单

#9


Put long running tasks (open database connections, connect to app server, etc) that have to be performed on startup in a thread. Any functionality that depends on these tasks are disabled until the thread is done.

放置必须在线程启动时执行的长时间运行的任务(打开数据库连接,连接到应用服务器等)。在线程完成之前,将禁用依赖于这些任务的任何功能。

It's a bit of a cheat, though. The main form comes up right away, but you're only giving the appearance of faster startup time.

不过,这有点像作弊。主要形式立即出现,但你只是给出了更快的启动时间。

#10


Compress your executable and any dlls using something like ASPack or UPX. Decompression time is more than made up for by faster load time.

使用ASPack或UPX等压缩您的可执行文件和任何dll。减速时间可以通过更快的加载时间来弥补。

UPX was used as an example of how to load FireFox faster.

UPX用作如何加快FireFox加载的示例。

Note that there are downsides to exe compression.

请注意,exe压缩存在缺点。

#11


This is just for the IDE, but Chris Hesick made a blog posting about increasing startup performance under the debugger.

这仅适用于IDE,但Chris Hesick发布了一篇关于在调试器下提高启动性能的博客文章。

#1


Try doing as little as possible in your main form's OnCreate event. Rather move some initialization to a different method and do it once the form is shown to the user. An indicator that the app is busy with a busy mouse cursor goes a long way.

尝试在主窗体的OnCreate事件中尽可能少地做。而是将一些初始化移动到不同的方法,并在向用户显示表单后执行此操作。应用程序忙于忙碌鼠标光标的指示器很长。

Experiments done shows that if you take the exact same application and simply add a startup notification to it, users actually perceive that app as starting up faster!

完成的实验表明,如果您使用完全相同的应用程序并只是向其添加启动通知,用户实际上会认为该应用程序启动速度更快!

Other than that you can do the usual things like exclude debug information and enable optimization in the compiler.

除此之外,您可以执行常规操作,例如排除调试信息并在编译器中启用优化。

On top of that, don't auto create all your forms. Create them dynamically as you need them.

最重要的是,不要自动创建所有表单。根据需要动态创建它们。

#2


In the project options, don't auto-create all of your forms up front. Create and free them as needed.

在项目选项中,不要预先自动创建所有表单。根据需要创建并释放它们。

#3


Well, as Argalatyr suggested I change my comment to a separate answer:

好吧,正如Argalatyr建议我将我的评论改为单独的答案:

As an extension to the "don't auto create forms" answer (which will be quite effective by itself) I suggest to delay opening connections to databases, internet, COM servers and any peripheral device until you need it first.

作为“不自动创建表单”答案的扩展(这本身就非常有效)我建议延迟打开与数据库,互联网,COM服务器和任何外围设备的连接,直到您首先需要它为止。

#4


Three things happen before your form is shown:

在您的表单显示之前会发生三件事:

  1. All 'initialization' blocks in all units are executed in "first seen" order.
  2. 所有单元中的所有“初始化”块都以“第一次看到”的顺序执行。

  3. All auto-created forms are created (loaded from DFM files and their OnCreate handler is called)
  4. 创建所有自动创建的表单(从DFM文件加载并调用其OnCreate处理程序)

  5. You main form is displayed (OnShow and OnActivate are called).
  6. 显示主窗体(调用OnShow和OnActivate)。

As other have pointed out, you should auto-create only small number of forms (especially if they are complicated forms with lots of component) and should not put lengthy processing in OnCreate events of those forms. If, by chance, your main form is very complicated, you should redesign it. One possibility is to split main form into multiple frames which are loaded on demand.

正如其他人所指出的,你应该只自动创建少量的表单(特别是如果它们是具有大量组件的复杂表单),并且不应该在这些表单的OnCreate事件中进行冗长的处理。如果您的主要形式非常复杂,您应该重新设计它。一种可能性是将主表单拆分为多个按需加载的帧。

It's also possible that one of the initialization blocks is taking some time to execute. To verify, put a breakpoint on the first line of your program (main 'begin..end' block in the .dpr file) and start the program. All initialization block will be executed and then the breakpoint will stop the execution.

其中一个初始化块也可能需要一些时间来执行。要验证,请在程序的第一行放置断点(.dpr文件中的主“begin..end”块)并启动程序。将执行所有初始化块,然后断点将停止执行。

In a similar way you can step (F8) over the main program - you'll see how long it takes for each auto-created form to be created.

以类似的方式,您可以在主程序上执行步骤(F8) - 您将看到创建每个自动创建的表单需要多长时间。

#5


Display a splash screen, so people won't notice the long startup times :).

显示启动画面,这样人们就不会注意到启动时间长:)。

#6


Fastest code - it's the code, that never runs. Quite obvious, really ;)

最快的代码 - 它是代码,永远不会运行。很明显,真的;)

#7


Deployment of the application can (and usually does!) happen in ways the developer may not have considered. In my experience this generates more performance issues than anyone would want.

应用程序的部署可以(通常会!)以开发人员可能没有考虑过的方式进行。根据我的经验,这会产生比任何人想要的更多性能问题。

A common bottleneck is file access - a configuration file, ini file that is required to launch the application can perform well on a developer machine, but perform abysmally in different deployment situations. Similarly, application logging can impede performance - whether for file access reasons or log file growth.

常见的瓶颈是文件访问 - 启动应用程序所需的配置文件,ini文件可以在开发人员计算机上很好地执行,但在不同的部署情况下执行得非常糟糕。同样,应用程序日志记录会阻碍性能 - 无论是出于文件访问原因还是日志文件增长。

What I see so often are rich-client applications deployed in a Citrix environment, or on a shared network drive, where the infrastructure team decides that user temporary files or personal files be stored in a location that the application finds issues with, and this leads to performance or stability issues.

我经常看到的是在Citrix环境或共享网络驱动器上部署的富客户端应用程序,基础架构团队决定将用户临时文件或个人文件存储在应用程序发现问题的位置,这导致性能或稳定性问题。

Another issue I often see affecting application performance is the method used to import and export data to files. Commonly in Delphi business applications I see export functions that work off DataSets - iterating and writing to file. Consider the method used to write to file, consider memory available, consider that the 'folder' being written to/read from may be local to the machine, or it may be on a remote server.

我经常看到影响应用程序性能的另一个问题是用于将数据导入和导出到文件的方法。通常在Delphi业务应用程序中,我看到可以使用DataSet的导出函数 - 迭代和写入文件。考虑用于写入文件的方法,考虑可用的内存,考虑写入/读取的“文件夹”可以是机器本地的,也可以是远程服务器上的。

A developer may argue that these are installation issues, outside the scope of their concern. I usually see many cycles of developer analysis on this sort of issue before it is identified as an 'infrastructure issue'.

开发人员可能会认为这些是安装问题,超出了他们关注的范围。在将此类问题确定为“基础架构问题”之前,我通常会看到许多开发人员分析周期。

#8


  • First thing to do is to clear auto created forms list (look for Project Options). Create forms on the fly when needed, especially if the application uses database connection (datamodule) or forms that include heavy use of controls.
  • 首先要做的是清除自动创建的表单列表(查找项目选项)。在需要时动态创建表单,尤其是在应用程序使用数据库连接(数据模块)或包含大量控件的表单时。

  • Consider using form inheritance also to decrease exe size (resource usage is mimized)
  • 考虑使用表单继承也减少exe大小(资源使用被模仿)

  • Decrease number of forms and merge similar or related functionality into single form
  • 减少表单数量并将类似或相关的功能合并为单个表单

#9


Put long running tasks (open database connections, connect to app server, etc) that have to be performed on startup in a thread. Any functionality that depends on these tasks are disabled until the thread is done.

放置必须在线程启动时执行的长时间运行的任务(打开数据库连接,连接到应用服务器等)。在线程完成之前,将禁用依赖于这些任务的任何功能。

It's a bit of a cheat, though. The main form comes up right away, but you're only giving the appearance of faster startup time.

不过,这有点像作弊。主要形式立即出现,但你只是给出了更快的启动时间。

#10


Compress your executable and any dlls using something like ASPack or UPX. Decompression time is more than made up for by faster load time.

使用ASPack或UPX等压缩您的可执行文件和任何dll。减速时间可以通过更快的加载时间来弥补。

UPX was used as an example of how to load FireFox faster.

UPX用作如何加快FireFox加载的示例。

Note that there are downsides to exe compression.

请注意,exe压缩存在缺点。

#11


This is just for the IDE, but Chris Hesick made a blog posting about increasing startup performance under the debugger.

这仅适用于IDE,但Chris Hesick发布了一篇关于在调试器下提高启动性能的博客文章。