Einzelnen Beitrag anzeigen

Der schöne Günther

Registriert seit: 6. Mär 2013
6.110 Beiträge
 
Delphi 10 Seattle Enterprise
 
#6

AW: TParallel.&For Problem unter Windows

  Alt 1. Okt 2019, 16:14
Das ändert sehr wohl was.

Ergebnisse auf meinem Rechner.

Dein Quelltext 1:1 übernommen:
Multithreaded: 4430 ms total
Singlethreaded: 1085 ms total

Closure berichtigt sodass die Schleifenvariablen _yy und _xx in der lokalen Methode liegen und nicht "außerhalb":
Multithreaded: 2020 ms total
Singlethreaded: 810 ms total

Random() gegen feste Zuweisung ersetzt:
Multithreaded: 447 ms total
Singlethreaded: 1100 ms total (wtf)

Von Debug 64 Bit auf Release-Fassung 64 Bit gewechselt:
Multithreaded: 225 ms
Singlethreaded: 590 ms


Delphi-Quellcode:
unit Unit1;

interface uses
   System.SysUtils,
   System.Types,
   System.UITypes,
   System.Classes,
   System.Diagnostics,
   System.Threading,

   FMX.Types,
   FMX.Controls,
   FMX.Forms,
   FMX.Graphics,
   FMX.Dialogs,
   FMX.Edit,
   FMX.EditBox,
   FMX.SpinBox,
   FMX.Layouts,
   FMX.ListBox,
   FMX.StdCtrls,
   FMX.Controls.Presentation;

type
   T3DFloatArray = TArray<TArray<TArray<Double>>>;

   TForm1 = class(TForm)
      Button1: TButton;
      ListBox1: TListBox;
      SpinBox1: TSpinBox;
      SpinBox2: TSpinBox;
      isMultiThreadedCheckbox: TCheckBox;
      procedure Button1Click(Sender: TObject);
      procedure FormCreate(Sender: TObject);
      private var
         stopwatch1, stopwatch2: TStopWatch;
      private
         class function NotRandom(): Double; inline;
   end;

var
   Form1: TForm1;


implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
var
   testDataDimensionCount: Integer;
   iterationCount: Integer;

   count, zz: Integer;
   testdata: T3DFloatArray;
   testProcedure: TProc<Integer>;
begin
   testDataDimensionCount := Round(SpinBox1.Value);
   iterationCount := Round(SpinBox2.Value);

   ListBox1.Clear();

   stopwatch1 := TStopwatch.StartNew();
   setlength(testdata, testDataDimensionCount, testDataDimensionCount, testDataDimensionCount);
   stopwatch1.Stop();

   ListBox1.Items.Add('get-mem: ' + inttostr(stopwatch1.ElapsedMilliseconds) + ' ms');

   testProcedure :=
      procedure(_zz: Integer)
      var
         _xx, _yy: Integer;
      begin
         for _yy := 0 to testDataDimensionCount - 1 do
            for _xx := 0 to testDataDimensionCount - 1 do
               testdata[zz, _yy, _xx] := NotRandom();
      end;

   stopwatch2 := TStopwatch.StartNew();
   for count := 0 to iterationCount - 1 do
      begin
         stopwatch1 := TStopwatch.StartNew();

         if isMultiThreadedCheckbox.IsChecked then
            begin
               TParallel.&For(
                  0,
                  testDataDimensionCount - 1,
                  testProcedure
               );
            end
         else
            begin
               for zz := 0 to testDataDimensionCount - 1 do
                  testProcedure(zz);
            end;

         stopwatch1.Stop();
         ListBox1.Items.Add('[' + inttostr(count) + ']: ' + inttostr(stopwatch1.ElapsedMilliseconds) + ' ms');
      end;

   stopwatch2.Stop;
   ListBox1.Items.Add('-> done: ' + inttostr(stopwatch2.ElapsedMilliseconds) + ' ms');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   randomize;
end;

class function TForm1.NotRandom(): Double;
begin
   Result := 42.0;
end;

end.
  Mit Zitat antworten Zitat