UnDiUdin
UnDiUdin

Reputation: 15384

Delphi project with many units takes a lot to run

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?

Upvotes: 6

Views: 848

Answers (5)

Jeroen Wiert Pluimers
Jeroen Wiert Pluimers

Reputation: 24503

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

Upvotes: 4

Bruce McGee
Bruce McGee

Reputation: 15334

There are a lot of good suggestions in this question.

You should absolutely make sure you aren't creating things on startup that you don't need right away. This usually the biggest launch delay on projects with a lot of forms.

In your case, is sounds like a lot of initialization code is being executed.

Upvotes: 1

Warren  P
Warren P

Reputation: 68922

You already know that if you have a lot of forms, try moving the forms out of the "auto create" list, and then add code, to create the forms when they are needed, but you are seeing this problem before you could even create a form. So, as others have said, initialization sections are the problem.

Jeroen's blog pointed me at a great resource for debugging this:

http://wiert.wordpress.com/2010/07/21/delphi-great-post-by-malcolm-groves-about-debugging-initialization-and-finalization-sections/

He pointed me to Malcom Groves:

http://www.malcolmgroves.com/blog/?p=649

Upvotes: 1

Arnaud Bouchez
Arnaud Bouchez

Reputation: 43033

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... :)

Upvotes: 1

Mikael Eriksson
Mikael Eriksson

Reputation: 138960

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.

Upvotes: 17

Related Questions