Skip to content

Commit 14b6b95

Browse files
committed
refactor: New version + score from Székely Balázs
1 parent c0fcff5 commit 14b6b95

File tree

2 files changed

+45
-52
lines changed

2 files changed

+45
-52
lines changed

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,18 @@ Submit your implementation and become part of the leader board!
7373
## Results
7474

7575
These are the results from running all entries into the challenge on my personal computer:
76+
- Ubuntu 23.10 64b
7677
- Ryzen 9 5950x 16 cores
7778
- 32GB RAM
7879
- 250GB SSD
7980
- 1TB HDD
8081

8182
| # | Result (m:s.ms): SSD | Result (m:s.ms): HDD | Submitter | Notes | Certificates |
8283
|---|----------------------|----------------------|---------------|-----------|--------------|
83-
| 1 | 1:4.925 | 2:29.476 | Székely Balázs aka GetMem | Using 16 threads | |
84+
| 1 | 0:29.212 | 2:2.504 | Székely Balázs | Using 16 threads | |
8485

8586
## Evaluating Results
8687

87-
Results are determined by running the program on:
88-
- Ryzen 9 5950x 16 core
89-
- 32GB RAM
90-
- 250GB SSD
91-
- 1TB HDD
92-
9388
Each contender is run 10 times in a row for both `SSD` and `HDD` using `hyperfine` for the time taking.
9489
The mean value of the 10 runs is the result for that contender and will be added to the results table above.
9590
The exact same `measurements.txt` file is used for evaluating all contenders.

entries/sbalazs/src/uweatherstations.pas

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
interface
66

77
uses
8-
Classes, SysUtils, syncobjs, contnrs;
8+
Classes, SysUtils, Math, syncobjs, contnrs;
99

1010
type
1111
TWSManager = class;
@@ -62,7 +62,7 @@ TWSThread = class(TWSThreadBase)
6262
FMS: TMemoryStream;
6363
procedure ProcessBuffer(ABuffer: String);
6464
procedure UpdateMainHashList;
65-
procedure AddToHashList(AStation, ATemp: String);
65+
procedure AddToHashList(AStation, ATemp: String); inline;
6666
procedure UpdateThreadList(AUpdateType: TUpdateType);
6767
protected
6868
procedure Execute; override;
@@ -155,6 +155,8 @@ function TWSThreadBase.GetLeftOver(AST: TStream): String;
155155
ReadCnt: LongInt;
156156
begin
157157
Result := '';
158+
if AST.Position = AST.Size then
159+
Exit;
158160
Len := 100 div SizeOf(Char);
159161
SetLength(Result, Len);
160162
ReadCnt := AST.Read(Pointer(Result)^, Len);
@@ -166,7 +168,6 @@ function TWSThreadBase.GetLeftOver(AST: TStream): String;
166168
begin
167169
Result := Copy(Result, 1, P - 1);
168170
AST.Position := AST.Position + Length(Result)*SizeOf(Char);
169-
Result := Result + sLineBreak + ' ';
170171
end;
171172
end;
172173
end;
@@ -257,17 +258,16 @@ procedure TWSThread.UpdateMainHashList;
257258
else
258259
begin
259260
WSAll := FWSManager.FHashListAll.Items[Index];
260-
if WS^.FMin < WSAll^.FMin then
261-
WSAll^.FMin := WS^.FMin;
262-
if WS^.FMax > WSALL^.FMax then
263-
WSAll^.FMax := WS^.FMax;
261+
WSAll^.FMin := Min(WSALL^.FMin, WS^.FMin);
262+
WSAll^.FMax := Max(WSAll^.FMax, WS^.FMax);
264263
WSAll^.FTot := WSAll^.FTot + WS^.FTot;
265264
WSAll^.FCnt := WSAll^.FCnt + WS^.FCnt;
266265
end;
267266
end;
268267
end;
269268

270269
procedure TWSThread.UpdateThreadList(AUpdateType: TUpdateType);
270+
271271
begin
272272
while (not Terminated) do
273273
begin
@@ -297,13 +297,14 @@ procedure TWSThread.AddToHashList(AStation, ATemp: String);
297297
WS: PWS;
298298
Index: Integer;
299299
Temp: Single;
300+
Code: Integer;
300301
begin
301302
Index := FHashList.FindIndexOf(AStation);
302303
if Index = -1 then
303304
begin
304-
ATemp := ATemp + '000000001';
305-
if not TryStrToFloat(ATemp, Temp) then
306-
Exit;
305+
Val(ATemp, Temp, Code);
306+
if Code <> 0 then Exit;
307+
307308
New(WS);
308309
WS^.FName := AStation;
309310
WS^.FMin := Temp;
@@ -314,64 +315,60 @@ procedure TWSThread.AddToHashList(AStation, ATemp: String);
314315
end
315316
else
316317
begin
317-
if not TryStrToFloat(ATemp, Temp) then
318-
Exit;
318+
Val(ATemp, Temp, Code);
319+
if Code <> 0 then Exit;
320+
319321
WS := FHashList.Items[Index];
320-
if Temp < WS^.FMin then
321-
WS^.FMin := Temp;
322-
if Temp > WS^.FMax then
323-
WS^.FMax := Temp;
322+
WS^.FMin := Min(WS^.FMin, Temp);
323+
WS^.FMax := Max(WS^.FMax, Temp);
324324
WS^.FTot := WS^.FTot + Temp;
325325
WS^.FCnt := WS^.FCnt + 1;
326326
end;
327327
end;
328328

329329
procedure TWSThread.ProcessBuffer(ABuffer: String);
330330
var
331-
SL: TStringList;
331+
P: Integer;
332+
PDel, PEnd: Integer;
332333
Str: String;
333-
P, I: Integer;
334334
Station: String;
335335
Temp: String;
336336
begin
337-
SL := TStringList.Create;
338-
try
339-
SL.DefaultEncoding := TEncoding.UTF8;
340-
SL.BeginUpdate;
341-
SL.Text := ABuffer;
342-
for I := 0 to SL.Count - 1 do
343-
begin
344-
Str := SL.Strings[I];
345-
P := Pos(';', Str);
346-
if P > 0 then
347-
begin
348-
Station := Copy(Str, 1, P - 1);
349-
Temp := Copy(Str, P + 1, Length(Str));
350-
AddToHashList(Trim(Station), Trim(Temp));
351-
end;
352-
end;
353-
SL.EndUpdate;
354-
finally
355-
SL.Free;
356-
end;
337+
Str := StringReplace(ABuffer, sLineBreak, ':', [rfReplaceAll]);
338+
if Str[1] = ':' then
339+
Delete(Str, 1, 1);
340+
P := 1;
341+
repeat
342+
PDel := Pos(';', Str, P);
343+
PEnd := Pos(':', Str, P);
344+
if PEnd = 0 then
345+
PEnd := Length(Str);
346+
Station := Copy(Str, P, PDel - P);
347+
Temp := Copy(Str, PDel + 1, PEnd - PDel - 1);
348+
if (Station <> '') and (Temp <> '') then
349+
AddToHashList(Station, Temp);
350+
P := PEnd + 1;
351+
until (PDel = 0) or (PEnd = 0);
357352
end;
358353

359354
procedure TWSThread.Execute;
360-
const
361-
BufferSize = 1048576;
362355
var
363356
Buffer: string;
364357
ReadCnt: LongInt;
358+
BufferSize: Int64;
365359
begin
366360
UpdateThreadList(utAdd);
367361
try
368362
FMS.Write(Pointer(FStr)^, Length(FStr) div SizeOf(Char));
369363
FMS.Position := 0;
370364
FStr := '';
365+
BufferSize := 1048576;
371366
repeat
372367
if Terminated then
373368
Break;
374369
Buffer := '';
370+
if BufferSize > FMS.Size - FMS.Position then
371+
BufferSize := FMS.Size - FMS.Position;
375372
SetLength(Buffer, BufferSize div SizeOf(Char));
376373
ReadCnt := FMS.Read(Pointer(Buffer)^, BufferSize div SizeOf(Char));
377374
if (ReadCnt > 0) then
@@ -407,7 +404,7 @@ function Compare(List: TStringList; Index1, Index2: Integer): Integer;
407404
begin
408405
Str1 := Copy(Str1, 1, P1 - 1);
409406
Str2 := Copy(Str2, 1, P2 - 1);
410-
Result := CompareText(Str1, Str2);
407+
Result := CompareStr(Str1, Str2);
411408
end;
412409
end;
413410

@@ -423,18 +420,18 @@ procedure TWSThreadsWatcher.CreateFinalList;
423420
try
424421
SL.DefaultEncoding := TEncoding.UTF8;
425422
SL.BeginUpdate;
426-
SL.CustomSort(@Compare);
427423
for I := 0 to FWSManager.FHashListAll.Count - 1 do
428424
begin
429425
WS := FWSManager.FHashListAll.Items[I];
426+
if Trim(WS^.FName) = '' then
427+
Continue;
428+
WS^.FTot := Round(WS^.FTot*10)/10;
430429
WS^.FMean := WS^.FTot/WS^.FCnt;
431-
WS^.FMean := Round(WS^.FMean*1000)/1000;
432430
Str := WS^.FName + '=' + FormatFloat('0.0', WS^.FMin) + '/' + FormatFloat('0.0', WS^.FMean) + '/' + FormatFloat('0.0', WS^.FMax) + ',';
433431
SL.Add(Str);
434432
end;
435-
SL.SortStyle := sslUser;
436-
SL.Sorted := True;
437433
SL.EndUpdate;
434+
SL.CustomSort(@Compare);
438435
Str := SL.Text;
439436
finally
440437
SL.Free;
@@ -477,6 +474,7 @@ procedure TWSThreadsWatcher.Execute;
477474
ReadCnt := FS.Read(Pointer(Buffer)^, BufferSize div SizeOf(Char));
478475
if (ReadCnt > 0) then
479476
begin
477+
if Terminated then Break;
480478
Buffer := Buffer + GetLeftOver(FS);
481479
WSThread := TWSThread.Create(Buffer, FWSManager);
482480
WSThread.Start;

0 commit comments

Comments
 (0)