theap.pp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. {
  2. Program to test heap functions, timing doesn't work
  3. }
  4. PROGRAM TestHeap;
  5. uses
  6. erroru;
  7. const
  8. {$ifdef cpusparc}
  9. Blocks = 1000;
  10. {$else}
  11. Blocks = 10000;
  12. {$endif}
  13. Procedure InitMSTimer;
  14. begin
  15. end;
  16. {Get MS Timer}
  17. Function MSTimer:longint;
  18. begin
  19. MSTimer:=0;
  20. end;
  21. procedure ShowHeap;
  22. var
  23. hstatus : TFPCHeapstatus;
  24. begin
  25. hstatus:=GetFPCHeapStatus;
  26. WriteLn ('Used: ', hstatus.CurrHeapUsed, ' Free: ', hstatus.CurrHeapFree,' Size: ',hstatus.CurrHeapSize);
  27. end;
  28. VAR Start, LoopTime,LoopTime2: LONGINT;
  29. Delta, TotalTime: LONGINT;
  30. L,Choice,K,T: WORD;
  31. BlkPtr: ARRAY [1..Blocks] OF POINTER;
  32. BlkSize: ARRAY [1..Blocks] OF WORD;
  33. Permutation: ARRAY [1..Blocks] OF WORD;
  34. BEGIN
  35. INitMSTimer;
  36. WriteLn ('Test of TP heap functions');
  37. WriteLn;
  38. TotalTime := 0;
  39. RandSeed := 997;
  40. ShowHeap;
  41. Start :=MSTimer;
  42. FOR L := 1 TO Blocks DO BEGIN
  43. END;
  44. LoopTime := MSTimer-Start;
  45. FOR L := 1 TO Blocks DO BEGIN
  46. BlkSize [L] := Random (512) + 1;
  47. END;
  48. Write ('Allocating ',Blocks,' blocks at the end of the heap: ');
  49. Start := MSTImer;
  50. FOR L := 1 TO Blocks DO BEGIN
  51. GetMem (BlkPtr [L], BlkSize [L]);
  52. END;
  53. Delta := MSTimer-Start-LoopTime;
  54. Inc (TotalTime, Delta);
  55. WriteLn (Delta:5, ' ms');
  56. ShowHeap;
  57. Write ('Deallocating same ',Blocks,' blocks in reverse order:');
  58. Start := MSTimer;
  59. FOR L := 1 TO Blocks DO BEGIN
  60. FreeMem (BlkPtr [L], BlkSize [L]);
  61. END;
  62. Delta := MSTimer-Start-LoopTime;
  63. Inc (TotalTime, Delta);
  64. WriteLn (Delta:5, ' ms');
  65. ShowHeap;
  66. Write ('Allocating ',Blocks,' blocks at the end of the heap: ');
  67. Start := MSTimer;
  68. FOR L := 1 TO Blocks DO BEGIN
  69. GetMem (BlkPtr [L], BlkSize [L]);
  70. END;
  71. Delta := MSTimer-Start-LoopTime;
  72. Inc (TotalTime, Delta);
  73. WriteLn (Delta:5, ' ms');
  74. ShowHeap;
  75. FOR L := 1 TO Blocks DO BEGIN
  76. Permutation [L] := L;
  77. END;
  78. Start := MSTimer;
  79. FOR L := Blocks DOWNTO 1 DO BEGIN
  80. Choice := Random (L)+1;
  81. K := Permutation [Choice];
  82. Permutation [Choice] := Permutation [L];
  83. END;
  84. LoopTime2 := MSTimer - Start;
  85. FOR L := 1 TO Blocks DO BEGIN
  86. Permutation [L] := L;
  87. END;
  88. Write ('Deallocating same ',Blocks,' blocks at random: ');
  89. Start := MSTimer;
  90. FOR L := Blocks DOWNTO 1 DO BEGIN
  91. Choice := Random (L)+1;
  92. K := Permutation [Choice];
  93. Permutation [Choice] := Permutation [L];
  94. FreeMem (BlkPtr [K], BlkSize [K]);
  95. END;
  96. Delta := MSTimer - Start - LoopTime2;
  97. Inc (TotalTime, Delta);
  98. WriteLn (Delta:5, ' ms');
  99. ShowHeap;
  100. Write ('Allocating ',Blocks,' blocks at the end of the heap: ');
  101. Start := MSTimer;
  102. FOR L := 1 TO Blocks DO BEGIN
  103. GetMem (BlkPtr [L], BlkSize [L]);
  104. END;
  105. Delta := MSTimer-Start-LoopTime;
  106. Inc (TotalTime, Delta);
  107. WriteLn (Delta:5, ' ms');
  108. ShowHeap;
  109. FOR L := 1 TO Blocks DO BEGIN
  110. Permutation [L] := L;
  111. END;
  112. Start := MSTimer;
  113. FOR L := Blocks DOWNTO 1 DO BEGIN
  114. Choice := Random (L)+1;
  115. K := Permutation [Choice];
  116. T:= Permutation [L];
  117. Permutation [L] := Permutation [Choice];
  118. Permutation [Choice] := T;
  119. END;
  120. LoopTime2 := MSTimer - Start;
  121. FOR L := 1 TO Blocks DO BEGIN
  122. Permutation [L] := L;
  123. END;
  124. Write ('Deallocating ',(Blocks div 2 + 1),' blocks at random: ');
  125. Start := MSTimer;
  126. FOR L := Blocks DOWNTO (Blocks div 2 + 1) DO BEGIN
  127. Choice := Random (L)+1;
  128. K := Permutation [Choice];
  129. T:= Permutation [L];
  130. Permutation [L] := Permutation [Choice];
  131. Permutation [Choice] := T;
  132. SYSTEM.FreeMem (BlkPtr [K], BlkSize [K]);
  133. END;
  134. Delta := MSTimer-Start-LoopTime2;
  135. Inc (TotalTime, Delta);
  136. WriteLn (Delta:5, ' ms');
  137. ShowHeap;
  138. Write ('Reallocating deallocated ',(Blocks div 2 + 1),' blocks at random: ');
  139. Start := MSTimer;
  140. FOR L := (Blocks div 2+1) TO Blocks DO BEGIN
  141. GetMem (BlkPtr [Permutation [L]], BlkSize [Permutation [L]]);
  142. END;
  143. Delta := MSTimer-Start-LoopTime;
  144. Inc (TotalTime, Delta);
  145. WriteLn (Delta:5, ' ms');
  146. ShowHeap;
  147. Write ('Deallocating all ',Blocks,' blocks at random: ');
  148. Start := MSTimer;
  149. FOR L := Blocks DOWNTO 1 DO BEGIN
  150. FreeMem (BlkPtr [L], BlkSize [L]);
  151. END;
  152. Delta := MSTimer-Start-LoopTime;
  153. Inc (TotalTime, Delta);
  154. WriteLn (Delta:5, ' ms');
  155. ShowHeap;
  156. WriteLn;
  157. WriteLn ('Total time for benchmark: ', TotalTime, ' ms');
  158. END.