Skip to content

Commit 89c6dff

Browse files
committed
feat: Results table gen now formats time
I'm still struggling with the sorting of the array
1 parent 5bb2560 commit 89c6dff

File tree

4 files changed

+159
-27
lines changed

4 files changed

+159
-27
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
unit Utilities.ArraySort;
2+
3+
{$mode objfpc}{$H+}
4+
5+
interface
6+
7+
type
8+
TCompareFunc = function (const elem1, elem2): Integer of object;
9+
10+
procedure AnySort(var Arr; Count: Integer; Stride: Integer; CompareFunc: TCompareFunc);
11+
12+
implementation
13+
14+
type
15+
TByteArray = array [Word] of byte;
16+
PByteArray = ^TByteArray;
17+
18+
procedure AnyQuickSort(var Arr; idxL, idxH: Integer;
19+
Stride: Integer; CompareFunc: TCompareFunc; var SwapBuf, MedBuf);
20+
var
21+
ls,hs : Integer;
22+
li,hi : Integer;
23+
mi : Integer;
24+
ms : Integer;
25+
pb : PByteArray;
26+
begin
27+
pb:=@Arr;
28+
li:=idxL;
29+
hi:=idxH;
30+
mi:=(li+hi) div 2;
31+
ls:=li*Stride;
32+
hs:=hi*Stride;
33+
ms:=mi*Stride;
34+
Move(pb[ms], medBuf, Stride);
35+
repeat
36+
while CompareFunc( pb[ls], medBuf) < 0 do begin
37+
inc(ls, Stride);
38+
inc(li);
39+
end;
40+
while CompareFunc( medBuf, pb[hs] ) < 0 do begin
41+
dec(hs, Stride);
42+
dec(hi);
43+
end;
44+
if ls <= hs then begin
45+
Move(pb[ls], SwapBuf, Stride);
46+
Move(pb[hs], pb[ls], Stride);
47+
Move(SwapBuf, pb[hs], Stride);
48+
// begin fix 11/11/2021: update ms if the reference point is moved
49+
if li=mi then ms:=hs;
50+
if hi=mi then ms:=ls;
51+
// end fix
52+
inc(ls, Stride); inc(li);
53+
dec(hs, Stride); dec(hi);
54+
end;
55+
until ls>hs;
56+
if hi>idxL then AnyQuickSort(Arr, idxL, hi, Stride, CompareFunc, SwapBuf, MedBuf);
57+
if li<idxH then AnyQuickSort(Arr, li, idxH, Stride, CompareFunc, SwapBuf, MedBuf);
58+
end;
59+
60+
procedure AnySort(var Arr; Count: Integer; Stride: Integer; CompareFunc: TCompareFunc);
61+
var
62+
buf: array of byte;
63+
begin
64+
if Count <= 1 then Exit; // should be more than 1 to be sortable
65+
SetLength(buf, Stride*2);
66+
AnyQuickSort(Arr, 0, Count-1, Stride, compareFunc, buf[0], buf[Stride]);
67+
end;
68+
69+
end.

utilities/results_generator/Common/resultsgenerator.common.pas

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ TResults = class(TObject)
2222
FResult: THyperfineResult;
2323

2424
function GenerateProgressBar(APBPosition, APBMax, APBWIdth: Integer): String;
25+
function CompareResults(const elem1, elem2): Integer;
26+
function FormatTime(ATime: Double): String;
2527
protected
2628
public
2729
constructor Create(AConfigFile: String);
@@ -36,8 +38,21 @@ implementation
3638
uses
3739
fpjson
3840
, jsonparser
41+
, Math
42+
, Utilities.ArraySort
3943
;
4044

45+
type
46+
//PResult = ^TResult;
47+
TResult = record
48+
Name: TJSONStringType;
49+
Notes: TJSONStringType;
50+
Compiler: TJSONStringType;
51+
Result: Double;
52+
Count: Integer;
53+
end;
54+
TResultsArray = array of TResult;
55+
4156
const
4257
lineBreak = #13;
4358

@@ -71,24 +86,52 @@ destructor TResults.Destroy;
7186
inherited Destroy;
7287
end;
7388

74-
procedure TResults.Generate;
75-
type
76-
TResult = record
77-
Result: Double;
78-
Count: Integer;
79-
Compiler: String;
89+
function TResults.CompareResults(const elem1, elem2): Integer;
90+
var
91+
i1 : TResult absolute elem1;
92+
i2 : TResult absolute elem2;
93+
begin
94+
if i1.Result = i2.Result then Result:= 0
95+
else if i1.Result < i2.Result then Result:= -1
96+
else Result:= 1;
97+
end;
98+
99+
function TResults.FormatTime(ATime: Double): String;
100+
var
101+
intPart, minutes: Integer;
102+
millis: String;
103+
begin
104+
Result:= '';
105+
intPart:= Trunc(ATime);
106+
minutes:= 0;
107+
if intPart > 60 then
108+
begin
109+
repeat
110+
Inc(minutes);
111+
Dec(intPart, 60);
112+
until intPart < 60;
80113
end;
81-
TResultsArray = array of TResult;
114+
millis:= Copy(
115+
FloatToStr(ATime),
116+
Pos('.', FloatToStr(ATime)) + 1,
117+
4
118+
);
119+
Result:= Format('%d:%d.%s',[
120+
minutes,
121+
intPart,
122+
millis
123+
]);
124+
end;
82125

126+
procedure TResults.Generate;
83127
var
84-
index, index1: Integer;
128+
index, index1, index2: Integer;
85129
content, hyperfineFile: String;
86130
results: TResultsArray;
87131
hyperfineStream: TFileStream;
88132
hyperfineJSON: TJSONData;
89133
begin
90-
content:= '';
91-
SetLength(results, FConfig.Entries.Count);
134+
SetLength(results, 0);
92135

93136
for index:= 0 to Pred(FConfig.Entries.Count) do
94137
begin
@@ -99,6 +142,8 @@ TResult = record
99142
'-1_000_000_000-SSD.json'
100143
);
101144
if not FileExists(hyperfineFile) then continue;
145+
SetLength(results, Length(results) + 1);
146+
index2:= Length(results) - 1;
102147
hyperfineStream:= TFileStream.Create(
103148
hyperfineFile,
104149
fmOpenRead
@@ -112,26 +157,20 @@ TResult = record
112157
try
113158
if FConfig.Entries[index].Compiler = 'fpc' then
114159
begin
115-
results[index].Compiler:= 'lazarus-3.0, fpc-3.2.2';
160+
results[index2].Compiler:= 'lazarus-3.0, fpc-3.2.2';
116161
end;
117162

118-
results[index].Result:= 0.0;
119-
results[index].Count:= 0;
163+
results[index2].Name:= FConfig.Entries[index].Name;
164+
results[index2].Notes:= FConfig.Entries[index].Notes;
165+
results[index2].Result:= 0.0;
166+
results[index2].Count:= 0;
120167
for index1:= Low(FResult.Times) to High(FResult.Times) do
121168
begin
122169
if (time = FResult.Max) or (time = FResult.Max) then continue;
123-
results[index].Result:= results[index].Result + FResult.Times[index1];
124-
Inc(results[index].Count);
170+
results[index2].Result:= results[index2].Result + FResult.Times[index1];
171+
Inc(results[index2].Count);
125172
end;
126-
results[index].Result:= results[index].Result / results[index].Count;
127-
{ #todo 99 -ogcarreno : needs to be done after sorting array by time }
128-
content:= content + Format('| %d | %.2f | %s | %s | %s | |'+LineEnding, [
129-
index,
130-
results[index].Result,
131-
results[index].Compiler,
132-
FConfig.Entries[index].Name,
133-
FConfig.Entries[index].Notes
134-
]);
173+
results[index2].Result:= results[index2].Result / results[index2].Count;
135174
finally
136175
FResult.Free;
137176
end;
@@ -145,8 +184,22 @@ TResult = record
145184

146185
WriteLn;
147186
WriteLn;
187+
188+
//AnySort(results, Length(results), SizeOf(TResult), @CompareResults);
189+
190+
content:= '';
191+
for index:= Low(results) to High(results) do
192+
begin
193+
content:= content + Format('| %d | %s | %s | %s | %s | |'+LineEnding, [
194+
index + 1,
195+
FormatTime(results[index].Result),
196+
results[index].Compiler,
197+
results[index].Name,
198+
results[index].Notes
199+
]);
200+
end;
201+
148202
Write(cTableHeader, content);
149-
// The results
150203
WriteLn;
151204
end;
152205

utilities/results_generator/Lazarus/src/resultsgenerator.lpi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
<RunParams>
8686
<FormatVersion Value="2"/>
8787
</RunParams>
88-
<Units Count="6">
88+
<Units Count="7">
8989
<Unit0>
9090
<Filename Value="resultsgenerator.lpr"/>
9191
<IsPartOfProject Value="True"/>
@@ -116,6 +116,11 @@
116116
<IsPartOfProject Value="True"/>
117117
<UnitName Value="Utilities.Data.Hyperfine"/>
118118
</Unit5>
119+
<Unit6>
120+
<Filename Value="../../../common/utilities.arraysort.pas"/>
121+
<IsPartOfProject Value="True"/>
122+
<UnitName Value="Utilities.ArraySort"/>
123+
</Unit6>
119124
</Units>
120125
</ProjectOptions>
121126
<CompilerOptions>
@@ -128,6 +133,11 @@
128133
<OtherUnitFiles Value="../../Common;../../../common"/>
129134
<UnitOutputDirectory Value="../../../../bin/lib/$(TargetCPU)-$(TargetOS)"/>
130135
</SearchPaths>
136+
<Linking>
137+
<Debugging>
138+
<DebugInfoType Value="dsDwarf3"/>
139+
</Debugging>
140+
</Linking>
131141
</CompilerOptions>
132142
<Debugging>
133143
<Exceptions Count="3">

utilities/results_generator/Lazarus/src/resultsgenerator.lpr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
, SysUtils
1111
, CustApp
1212
, ResultsGenerator.Console
13-
, ResultsGenerator.Common, Utilities.Data.Hyperfine
13+
, ResultsGenerator.Common
1414
;
1515

1616
{$I version.inc}

0 commit comments

Comments
 (0)