tqsort_killer.pp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. {$mode objfpc} {$h+} {$typedaddress on} {$modeswitch advancedrecords} {$coperators on} {$modeswitch anonymousfunctions}
  2. uses
  3. SysUtils, Generics.Collections, Generics.Defaults;
  4. var
  5. anythingWrong: boolean = false;
  6. generic procedure Swap<Ty>(var a, b: Ty);
  7. var
  8. temp: Ty;
  9. begin
  10. temp := a; a := b; b := temp;
  11. end;
  12. type
  13. generic SortBenchmark<Ty> = record
  14. type
  15. CreateProc = function(id: SizeUint): Ty;
  16. TyArray = array of Ty;
  17. class procedure Run(create: CreateProc; const tyPlural: string; lenMul: double); static;
  18. class procedure BenchSort(const src, ref: array of Ty; var prevTime: double); static;
  19. // Built against specific QSort implementation that uses (L + R) div 2 as a median, and won't kill any other, even one that uses (L + R + 1) div 2.
  20. // 'ref' must be sorted and contain no duplicates.
  21. class function BuildQSortKiller(const ref: array of Ty): TyArray; static;
  22. end;
  23. class procedure SortBenchmark.Run(create: CreateProc; const tyPlural: string; lenMul: double);
  24. type
  25. OrderEnum = (RandomOrder, QSortKillerOrder);
  26. const
  27. OrderNames: array[OrderEnum] of string = ('random', 'QSort killer');
  28. var
  29. ref, src: TyArray;
  30. i, lenBase, len: SizeInt;
  31. cmp: specialize IComparer<Ty>;
  32. srcOrder: OrderEnum;
  33. msg: string;
  34. prevTime: double;
  35. begin
  36. writeln('--- ', tyPlural, ' ---', LineEnding);
  37. for srcOrder in OrderEnum do
  38. begin
  39. writeln('Order: ', OrderNames[srcOrder], '.');
  40. prevTime := -1;
  41. for lenBase in specialize TArray<SizeInt>.Create(10 * 1000, 20 * 1000, 40 * 1000) do
  42. begin
  43. len := round(lenMul * lenBase);
  44. SetLength((@ref)^, len);
  45. cmp := specialize TComparer<Ty>.Default;
  46. for i := 0 to len - 1 do
  47. begin
  48. ref[i] := create(i);
  49. if (i > 0) and (cmp.Compare(ref[i - 1], ref[i]) >= 0) then
  50. begin
  51. writeln('''create'' callback must return ', tyPlural, ' in strictly ascending order.');
  52. anythingWrong := true;
  53. exit;
  54. end;
  55. end;
  56. case srcOrder of
  57. RandomOrder:
  58. begin
  59. RandSeed := 1;
  60. src := Copy(ref);
  61. for i := len - 1 downto 1 do
  62. specialize Swap<Ty>(src[i], src[random(int64(i))]);
  63. end;
  64. QSortKillerOrder:
  65. src := BuildQSortKiller(ref);
  66. end;
  67. WriteStr(msg, 'n = ', len, ': ');
  68. write(msg.PadRight(12));
  69. BenchSort(src, ref, prevTime);
  70. end;
  71. writeln;
  72. end;
  73. end;
  74. class function SortBenchmark.BuildQSortKiller(const ref: array of Ty): TyArray;
  75. var
  76. ris: array of SizeInt;
  77. i: SizeInt;
  78. begin
  79. SetLength((@ris)^, length(ref)); // Swaps that QSort would perform are tracked here, to build the worst case possible. >:3
  80. for i := 0 to High(ris) do ris[i] := i;
  81. SetLength((@result)^, length(ref));
  82. for i := 0 to High(ref) do
  83. begin
  84. specialize Swap<SizeInt>(ris[i], ris[i + (High(ref) - i) shr 1]);
  85. result[ris[i]] := ref[i];
  86. end;
  87. end;
  88. class procedure SortBenchmark.BenchSort(const src, ref: array of Ty; var prevTime: double);
  89. var
  90. arr: TyArray;
  91. startTime: TDateTime;
  92. time, timePassed: double;
  93. i: SizeInt;
  94. reps: cardinal;
  95. begin
  96. startTime := Now;
  97. reps := 0;
  98. repeat
  99. arr := Copy(src);
  100. specialize TArrayHelper<Ty>.Sort(arr);
  101. timePassed := (Now - startTime) * SecsPerDay;
  102. reps += 1;
  103. until not (timePassed < 0.5);
  104. time := timePassed / reps;
  105. write(time * 1e3:0:1, ' ms/sort');
  106. if prevTime > 0 then write(' (', time / prevTime:0:1, 'x from previous)');
  107. if time / prevTime > 3 then
  108. begin
  109. writeln;
  110. writeln('Bad sorting algorithm behaviour');
  111. halt(1);
  112. end;
  113. prevTime := time;
  114. write(', ');
  115. for i := 0 to High(ref) do
  116. if arr[i] <> ref[i] then
  117. begin
  118. writeln('FAIL @ ', i, ' / ', length(ref));
  119. anythingWrong := true;
  120. exit;
  121. end;
  122. writeln('OK');
  123. end;
  124. begin
  125. specialize SortBenchmark<string>.Run(
  126. function(id: SizeUint): string
  127. begin
  128. SetLength((@result)^, 5);
  129. result[5] := char(ord('A') + id mod 26); id := id div 26;
  130. result[4] := char(ord('A') + id mod 26); id := id div 26;
  131. result[3] := char(ord('A') + id mod 26); id := id div 26;
  132. result[2] := char(ord('A') + id mod 26); id := id div 26;
  133. result[1] := char(ord('A') + id mod 26);
  134. end, 'strings', 0.15);
  135. specialize SortBenchmark<single>.Run(
  136. function(id: SizeUint): single
  137. begin
  138. result := -1000 + id / 1000;
  139. end, 'float32''s', 1.0);
  140. if anythingWrong then writeln(LineEnding, 'Something was wrong, see above.');
  141. end.