Home > OS >  Delphi - Measure execution time per line of an unit
Delphi - Measure execution time per line of an unit

Time:12-08

Currently I am working on performance improvements in a Delphi calculation module (bpl). In past days, I found several slow code lines. We improved execution time from 8 to 3 minutes.

I found slow code lines by adding stopwatches all over the units, but making these changes is time consuming.

procedure TCalculator.Execute;
begin
  TStopwatchWrapper.Start(1);
  CollectValues;
  TStopwatchWrapper.Pause(1);
  TStopwatchWrapper.Start(2);
  CalculateOrderLines;
  TStopwatchWrapper.Pause(2);
  ...
  TStopwatchWrapper.ShowTimes;

end;

procedure TCalculator.CollectValues;
begin
  for {..} do
  begin
    {more timing}
  end;
end;

The calculation units are hundreds of lines.

I would love to be able to decorate an unit, to find these slow code lines. I was thinking of Spring4D interceptors, but this only works for intercepting the external calls.

Is it possible to measure cumulative execution time per line or procedure? Without adding stopwatches or profiling calls all over the code.

Thank you in advance.

CodePudding user response:

Personally, I recommend using SamplingProfiler (the website is quite outdated - it works fine with executables from the latest Delphi version, it might not just pick up the search paths automatically). It usually works flawlessly with a single executable (needs either td32 or map file) but not so well with additional runtime packages or DLLs.

For that, I recommend using Intel VTune (completely free to use). For that to work you need to produce pdb files for any binary you want proper information (otherwise it will just report addresses). That can be done with map2pdb.

If you have an AMD CPU you might want to try AMD μProf - I have no personal experience with it but from what I have heared it's not as good as VTune.

  • Related