Recent

Author Topic: Performance difference between loading component and creating runtime  (Read 414 times)

egsuh

  • Sr. Member
  • ****
  • Posts: 433
Is there any performance differences between loading pre-defined component and creating it runtime?

As an example, we can define a function in both ways .

Code: Pascal  [Select][+][-]
  1. interface
  2. TdmDB = class(TDataModule)
  3.     AQuery: TSQLQuery;
  4.  
  5.    function A : integer;
  6.    function C : integer;
  7. end;
  8.  
  9. implementation
  10.  
  11. function TdmDB.A: integer;
  12. begin
  13.     AQuery.SQL.Text := 'select a from table_1 where b=5';
  14.    Result := AQuery.Fields[0].AsInteger;
  15. end;
  16.  
  17. // or
  18.  
  19. function TdmDB.C: integer;
  20. var
  21.    xqr : TSQLQuery;
  22. begin
  23.     xqr := TSQLQuery.create(nil);
  24.     xqr.DataBAse := //proper database;
  25.     xqr.SQL.Text := 'select a from table_1 where b=5';
  26.     Result := xqr.Fields[0].AsInteger;
  27.     xqr.Free;
  28. end;
  29.  

How much performance differences do you expect between function A and function C?

lucamar

  • Hero Member
  • *****
  • Posts: 3019
Very little, if anything, for normal components. With components linked to databases, like TSQLQuery I would expect maybe a little (barely appreciable) delay in the run-time creation for the link to become active (i.e. connecting to the database).

It would also happen for the loaded one but it might be missed as a general "wait for the application to start running". That is, supposing the component was set to "Active" at design-time; otherwise no difference should be appreciable.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2104
  • Compiler Developer
How much performance differences do you expect between function A and function C?

Loading using the streaming mechanism is probably slower, because the lfm needs to be parsed and the RTTI needs to be navigated while creating it at runtime will simply lead to calls. I don't know in how far it will be slower however.

That said: how often are you going to create your datamodule? If it's only created once at start the time that is needed for loading it from the resource will probably only be a small part of the whole startup of the LCL. Also if your application should be a long running one (e.g. a server) then it doesn't matter as much either as the startup time compared to the runtime will be small.

If you have performance issues better try to find out code that is "hot", meaning that it's often used and much time is spend in it and improve that.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8720
  • FPC developer.
How much performance differences do you expect between function A and function C?

Loading using the streaming mechanism is probably slower, because the lfm needs to be parsed and the RTTI needs to be navigated while creating it at runtime will simply lead to calls. I don't know in how far it will be slower however.

I'm not sure that is the case. To make form streaming order independent, a lot of events are not called while loading it. They might be called when manually creating it, unless you are very careful (and then often still implementation dependent)

 

TinyPortal © 2005-2018