Einzelnen Beitrag anzeigen

DieDolly

Registriert seit: 22. Jun 2018
2.175 Beiträge
 
#7

AW: Integer Array sortieren

  Alt 14. Mär 2019, 13:42
Ich habe dein Beispiel gerade mal nachgestellt.
2.000.000 Durchgänge mit einem 100er Integer-Array.

{1}: 3650 Millisekunden
{2}: 34955 Millisekunden
{2}: 34602 Millisekunden

Sehre unschöner Code aber das ist bei so einem kleinen Test mehr als egal.
Delphi-Quellcode:
var
 SW: TStopwatch;
 i: Integer;
 IntArray: TArray<Integer>;
 A, B, C: Integer;
begin
 SW := TStopwatch.StartNew;
 for i := 0 to 1999999 do
  begin
   IntArray := TArray<Integer>.Create(72, 59, 80, 78, 90, 34, 27, 23, 51, 25, 66, 69, 45, 35, 30, 16, 92, 73, 61, 18, 68, 39, 1, 84, 53, 93, 65, 42, 6, 54, 99, 46, 100, 24, 11, 87, 76,
    79, 56, 5, 22, 64, 8, 94, 95, 7, 21, 77, 71, 29, 12, 55, 20, 37, 49, 19, 52, 96, 9, 91, 57, 44, 67, 74, 88, 32, 75, 31, 28, 36, 89, 47, 82, 14, 3, 85, 58, 97, 60, 62, 2, 15, 17, 86,
    41, 98, 48, 81, 83, 40, 4, 63, 10, 50, 33, 70, 43, 38, 13, 26);

   TArray.Sort<Integer>(IntArray);
  end;
 SW.Stop;
 A := SW.ElapsedMilliseconds;

 //

 SW := TStopwatch.StartNew;
 for i := 0 to 1999999 do
  begin
   IntArray := TArray<Integer>.Create(72, 59, 80, 78, 90, 34, 27, 23, 51, 25, 66, 69, 45, 35, 30, 16, 92, 73, 61, 18, 68, 39, 1, 84, 53, 93, 65, 42, 6, 54, 99, 46, 100, 24, 11, 87, 76,
    79, 56, 5, 22, 64, 8, 94, 95, 7, 21, 77, 71, 29, 12, 55, 20, 37, 49, 19, 52, 96, 9, 91, 57, 44, 67, 74, 88, 32, 75, 31, 28, 36, 89, 47, 82, 14, 3, 85, 58, 97, 60, 62, 2, 15, 17, 86,
    41, 98, 48, 81, 83, 40, 4, 63, 10, 50, 33, 70, 43, 38, 13, 26);

   TArray.Sort<Integer>(IntArray, TComparer<Integer>.Construct(
    function(const A, B: Integer): Integer
    begin
     Result := A - B;
    end));
  end;
 SW.Stop;
 B := SW.ElapsedMilliseconds;

 //

 SW := TStopwatch.StartNew;
 for i := 0 to 1999999 do
  begin
   IntArray := TArray<Integer>.Create(72, 59, 80, 78, 90, 34, 27, 23, 51, 25, 66, 69, 45, 35, 30, 16, 92, 73, 61, 18, 68, 39, 1, 84, 53, 93, 65, 42, 6, 54, 99, 46, 100, 24, 11, 87, 76,
    79, 56, 5, 22, 64, 8, 94, 95, 7, 21, 77, 71, 29, 12, 55, 20, 37, 49, 19, 52, 96, 9, 91, 57, 44, 67, 74, 88, 32, 75, 31, 28, 36, 89, 47, 82, 14, 3, 85, 58, 97, 60, 62, 2, 15, 17, 86,
    41, 98, 48, 81, 83, 40, 4, 63, 10, 50, 33, 70, 43, 38, 13, 26);

   TArray.Sort<Integer>(IntArray, TComparer<Integer>.Construct(
    function(const A, B: Integer): Integer
    begin
     if A < B then
      Result := -1
     else if A = B then
      Result := 0
     else
      Result := 1;
    end));
  end;
 SW.Stop;
 C := SW.ElapsedMilliseconds;

 Caption := '{1}: ' + A.ToString + ', {2}: ' + B.ToString + ', {3}: ' + C.ToString;
end;
  Mit Zitat antworten Zitat