Advertisement
pmcgee

Swap Test Delphi

Sep 17th, 2019
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.34 KB | None | 0 0
  1. program SwapTest;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. // Initially from :  http://www.delphigroups.info/2/29/2921.html
  5. uses
  6.   System.Diagnostics;
  7.  
  8. procedure SwapPas(var Left, Right: Integer);
  9. var
  10.   temp: Integer;
  11. begin
  12.   temp  := Left;
  13.   Left  := Right;
  14.   Right := temp;
  15. end;
  16.  
  17. procedure SwapXor(var Left, Right: Integer);
  18. begin
  19.   Left  := Left  xor Right;
  20.   Right := Right xor Left;
  21.   Left  := Left  xor Right;
  22. end;
  23.  
  24. procedure SwapAsm(var Left, Right: Integer);
  25. asm
  26. {$IFDEF CPUX86}
  27.   xchg ecx, [eax]
  28.   xchg ecx, [edx]
  29.   xchg [eax], ecx
  30. {$ENDIF}
  31. {$IFDEF CPUX64}
  32.   xchg eax, [rcx]
  33.   xchg eax, [rdx]
  34.   xchg [rcx], eax
  35. {$ENDIF}
  36. end;
  37.  
  38. // -----------------------------------------------------------------------
  39.  
  40. type
  41.   TGeneric = class
  42.   public
  43.     class procedure Swap<T>(var Left, Right: T); static;
  44.   end;
  45.  
  46. class procedure TGeneric.Swap<T>(var Left, Right: T);
  47. var
  48.   temp: T;
  49. begin
  50.   temp := Left;
  51.   Left := Right;
  52.   Right := temp;
  53. end;
  54.  
  55.  
  56. {
  57.  
  58. For 32 bit with T = Pointer we have:
  59.  
  60. push ebx
  61. mov ecx,[eax]
  62. mov ebx,[edx]
  63. mov [eax],ebx
  64. mov [edx],ecx
  65. pop ebx
  66. ret
  67.  
  68. For 64 bit with T = Pointer we have:
  69.  
  70. mov rax,[rcx]
  71. mov r8,[rdx]
  72. mov [rcx],r8
  73. mov [rdx],rax
  74. ret
  75.  
  76. }
  77.  
  78. // -----------------------------------------------------------------------
  79.  
  80. const
  81.   N = 5000;
  82.  
  83. var
  84.   i, j: Integer;
  85.   A, B: array [1..5000] of integer;
  86.   //A, B: TArray<Integer>;
  87.   Stopwatch: TStopwatch;
  88.  
  89. begin
  90.   //SetLength(A, N);
  91.   //SetLength(B, N);
  92.   for i  := Low(A) to High(A) do begin
  93.     A[i] := i;
  94.     B[i] := N-i;
  95.   end;
  96.   //B := Copy(A);
  97.  
  98.   Stopwatch := TStopwatch.StartNew;
  99.   for j := 1 to 100000 do
  100.     for i := low(A) to high(A) do
  101.       SwapPas(A[i], B[i]);
  102.   Writeln('SwapPas: ', Stopwatch.ElapsedMilliseconds);
  103.  
  104.   Stopwatch := TStopwatch.StartNew;
  105.   for j := 1 to 100000 do
  106.     for i := low(A) to high(A) do
  107.       TGeneric.Swap(A[i], B[i]);
  108.   Writeln('SwapGen: ', Stopwatch.ElapsedMilliseconds);
  109.  
  110.   Stopwatch := TStopwatch.StartNew;
  111.   for j := 1 to 100000 do
  112.     for i := low(A) to high(A) do
  113.       SwapXor(A[i], B[i]);
  114.   Writeln('SwapXor: ', Stopwatch.ElapsedMilliseconds);
  115.  
  116.   Stopwatch := TStopwatch.StartNew;
  117.   for j := 1 to 100000 do
  118.     for i := low(A) to high(A) do
  119.       SwapAsm(A[i], B[i]);
  120.   Writeln('SwapAsm: ', Stopwatch.ElapsedMilliseconds);
  121.  
  122.   Writeln('**');
  123.   Stopwatch.Stop;
  124.   Readln;
  125. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement