I have a dpr with 290+ units.
The compiled exe is 50MB.
The dpr code is now like this:
begin
ShowMessage('Before Initialize');
Application.Initialize;
When I double click on the built exe I notice that 8 seconds pass before I see "Before Initialize". Is this because of big exe size? Or is there a way to minimize this time?
Before Application.Initialize every initialization section of every unit is executed. You might have some code in there that takes time.
The number of units is not the problem. I have a project with 1100+ units, exe is 35 MB and it starts instantaneously.
If you are starting from network drive or really slow disk you might experience a slowdown.
Based on your question, it can be anything.
The only advice I can give you is to measure:
Log the timestamps of every entry/exit in all your unit initialization sections.
Based on one of your comments (which you should add to your question as it describes more detail):
WindowsCodecs.dll is initialized by one of your units, probably converting one or more images from one format into another.
You should delay the conversion until the result of that conversion is needed.
--jeroen
The initialization section of the units is normally not a speed problem (unless you have some database-related stuff in there).
What could be slow is the TForm loading from resources.
It's always better to have the TForm created on the fly, only when it's necessary: - Go to "Project" menu, then select "Options", then the "Forms" tab. - Put all not mandatory forms from left list to the right "available" list. - Create the forms on request, by some code.
The unit remains the same:
type
TOneForm = class(TForm)
....
end;
var
OneForm: TOneForm;
But you can use the following code to create the form on request:
Instead of your former
OneForm.ShowModal;
uses this kind of code
if OneForm=nil then
OneForm := TOneForm.Create(Application);
OneForm.ShowModal;
You'll find the loading of the application much faster.
Note: I just read out that the problem was before form loading. So the above trick won't work for this particular problem. I keep the answer because it could be useful to others. I'll read better next time. :(
In all cases, having a lot of code run from the initialization is not a good design. It sounds like a lot of global objects or variables... refactoring could make sense here... :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With