Janilabo

Janilabo | TIAByRange()

Sep 7th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.69 KB | None | 0 0
  1. const
  2.   RUNS = 10;
  3.   RANGE_START = -9999;
  4.   RANGE_FINISH = 9999;
  5.  
  6. function TIAByRange8bit(aStart, aFinish: Integer): TIntArray;
  7. var
  8.   g, h, i, t: Integer;
  9. begin
  10.   if (aStart <> aFinish) then
  11.   begin
  12.     if (aStart > aFinish) then
  13.       Swap(aStart, aFinish);    
  14.     h := (aFinish - aStart);
  15.     g := (h div 8);
  16.     SetLength(Result, (h + 1));  
  17.     for i := 0 to g do
  18.     begin                              
  19.       t := (i * 4);
  20.       Result[t] := (aStart + t);
  21.       Result[(t + 1)] := ((aStart + t) + 1);
  22.       Result[(t + 2)] := ((aStart + t) + 2);
  23.       Result[(t + 3)] := ((aStart + t) + 3);      
  24.       Result[((h - t) - 3)] := ((aFinish - t) - 3);
  25.       Result[((h - t) - 2)] := ((aFinish - t) - 2);
  26.       Result[((h - t) - 1)] := ((aFinish - t) - 1);
  27.       Result[(h - t)] := (aFinish - t);  
  28.     end;    
  29.     if (((h + 1) mod 2) = 1) then
  30.       Result[(t + 1)] := (aStart + (t + 1));
  31.   end else
  32.     Result := [aStart];
  33. end;
  34.  
  35. function TIAByRange4bit(aStart, aFinish: Integer): TIntArray;
  36. var
  37.   g, h, i, t: Integer;
  38. begin
  39.   if (aStart <> aFinish) then
  40.   begin
  41.     if (aStart > aFinish) then
  42.       Swap(aStart, aFinish);    
  43.     h := (aFinish - aStart);
  44.     g := (h div 4);
  45.     SetLength(Result, (h + 1));  
  46.     for i := 0 to g do
  47.     begin                              
  48.       t := (i * 2);
  49.       Result[t] := (aStart + t);
  50.       Result[(t + 1)] := ((aStart + t) + 1);
  51.       Result[((h - t) - 1)] := ((aFinish - t) - 1);
  52.       Result[(h - t)] := (aFinish - t);  
  53.     end;    
  54.     if (((h + 1) mod 2) = 1) then
  55.       Result[(t + 1)] := (aStart + (t + 1));
  56.   end else
  57.     Result := [aStart];
  58. end;
  59.  
  60. function TIAByRange2bit(aStart, aFinish: Integer): TIntArray;
  61. var
  62.   g, h, i: Integer;
  63. begin
  64.   if (aStart <> aFinish) then
  65.   begin
  66.     if (aStart > aFinish) then
  67.       Swap(aStart, aFinish);    
  68.     h := (aFinish - aStart);
  69.     g := (h div 2);
  70.     SetLength(Result, (h + 1));
  71.     for i := 0 to g do
  72.     begin                      
  73.       Result[i] := (aStart + i);
  74.       Result[(h - i)] := (aFinish - i);
  75.     end;    
  76.     if (((h + 1) mod 2) = 1) then
  77.       Result[i] := (aStart + i);
  78.   end else
  79.     Result := [aStart];
  80. end;
  81.  
  82. function TIAByRange(aStart, aFinish: Integer): TIntArray;
  83. var
  84.   i: Integer;
  85. begin
  86.   if (aStart <> aFinish) then
  87.   begin
  88.     if (aStart > aFinish) then
  89.       Swap(aStart, aFinish);
  90.     SetLength(Result, ((aFinish - aStart) + 1));
  91.     for i := aStart to aFinish do
  92.       Result[(i - aStart)] := i;
  93.   end else
  94.     Result := [aStart];
  95. end;
  96.  
  97. var
  98.   i, tm: Integer;
  99.   tmp: TIntArray;
  100.   tmr: array[0..3] of TIntArray;
  101.   TSA: TStrArray;
  102.  
  103. begin
  104.   ClearDebug;
  105.   TSA := ['TIAByRange', 'TIAByRange2bit', 'TIAByRange4bit', 'TIAByRange8bit'];
  106.   for i := 0 to 3 do
  107.     SetLength(tmr[i], RUNS);
  108.   for i := 0 to (RUNS - 1) do
  109.   begin              
  110.     tm := GetSystemTime;
  111.     tmp := TIAByRange(RANGE_START, RANGE_FINISH);
  112.     tmr[0][i] := (GetSystemTime - tm);
  113.     SetLength(tmp, 0);
  114.     Wait(10);
  115.     tm := GetSystemTime;
  116.     tmp := TIAByRange2bit(RANGE_START, RANGE_FINISH);
  117.     SetLength(tmp, 0);
  118.     tmr[1][i] := (GetSystemTime - tm);    
  119.     Wait(10);
  120.     tm := GetSystemTime;
  121.     tmp := TIAByRange4bit(RANGE_START, RANGE_FINISH);
  122.     SetLength(tmp, 0);
  123.     tmr[2][i] := (GetSystemTime - tm);
  124.     Wait(10);
  125.     tm := GetSystemTime;
  126.     tmp := TIAByRange8bit(RANGE_START, RANGE_FINISH);
  127.     tmr[3][i] := (GetSystemTime - tm);
  128.     SetLength(tmp, 0);
  129.     Wait(10);
  130.   end;
  131.   for i := 0 to 3 do                                                        
  132.   begin
  133.     WriteLn(TSA[i] + ' avg speed: ' + FloatToStr(TIAMean(tmr[i])) + ' ms.');
  134.     SetLength(tmr[i], 0);
  135.   end;                  
  136.   SetLength(TSA, 0);
  137. end.
Advertisement
Add Comment
Please, Sign In to add comment