theap.pp 4.9 KB

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