Serious FireMonkey performance issues when there are a lot of controls at screen

I tried your code, it takes 00:10:439 on my PC on XE3 to fill screen with cells. By disabling these lines:

  //ProgressBar.Value:= ProgressBar.Value + 1;
  //Caption:= 'Elapsed time: ' + FormatDateTime('nn:ss:zzz', Time - T) +
  //          ' (min:sec:msec) Cells: ' + IntToStr(Trunc(ProgressBar.Value));
  ...
  //Application.ProcessMessages;

This goes down to 00:00:106 (!).

Updating visual controls (such as ProgressBar or Form.Caption) is very expensive. If you really think you need that, do that only every 100-th iteration, or better, only every 250 processor ticks.

If that does not help with performance, please run your code with these lines disabled and update the question.

Further, I’ve added code to test the repainting time:

T:= Time;
// Bruno Fratini:
// The following lines are required
// otherwise the cells won't be properly paint after maximizing
//BeginUpdate;
Invalidate;
//EndUpdate;
Application.ProcessMessages;
Caption := Caption + ', Repaint time: '+FormatDateTime('nn:ss:zzz', Time - T);

When run for a first time, creating all the controls takes 00:00:072, repainting takes 00:03:089. So it’s not the object management but the first time repainting which is slow.

Second time repainting is considerably faster.

Since there’s a discussion in comments, here’s how you do progress updates:

var LastUpdateTime: cardinal;
begin
  LastUpdateTime := GetTickCount - 250;
  for i := 0 to WorkCount-1 do begin
    //...
    //Do a part of work here

    if GetTickCount-LastUpdateTime > 250 then begin
      ProgressBar.Position := i;
      Caption := IntToStr(i) + ' items done.';
      LastUpdateTime := GetTickCount;
      Application.ProcessMessages; //not always needed
    end;
  end;
end;

Leave a Comment