Skip to content

Commit 0b167d9

Browse files
Merge branch 'gcarreno:main' into main
2 parents e9e20c3 + 094e830 commit 0b167d9

File tree

6 files changed

+1502
-12
lines changed

6 files changed

+1502
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ These are the results from running all entries into the challenge on my personal
162162
| 6 | 3:59.917 | lazarus-3.0, fpc-3.2.2 | Iwan Kelaiah | Using 1 thread | |
163163
| 7 | 7:2.726 | delphi 12.1 | David Cornelius | Using 1 threads | |
164164
| 8 | 7:9.974 | delphi 12.1 | Brian Fire | Using 1 threads | |
165-
| 🟨 | 0:1.697 | lazarus-3.99, fpc-3.3.1 | O Coddo | Yellow Card: use of `C/C++` libs | |
165+
| - | 0:1.697 | lazarus-3.99, fpc-3.3.1 | O Coddo | Now good. New results next run | |
166166
| 🟠 | 0:19.699 | lazarus-3.0, fpc-3.2.2 | Lurendrejer Aksen | Using 32 threads **(failed hash)** | |
167167

168168
> **NOTE**

entries/dtoepfl/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Daniel Töpfl
2+
3+
1. Be sure there is a valid measurements is available in .\data\measurements.txt !
4+
2. Open this project in Delphi
5+
3. Compile it as release!
6+
4. Execute the application (.\bin\dtpfl_1brc.dtoepfl-win-64.exe) as followed:
7+
```
8+
.\bin\dtpfl_1brc.dtoepfl-win-64.exe -i ..\data\measurements.txt
9+
```
10+
11+
## Acknowledgements
12+
13+
Thanks to [Gustavo 'Gus' Carreno](https://github.com/gcarreno) & [georges-hatem](https://github.com/georges-hatem)
14+
15+

entries/dtoepfl/src/dtoepfl.dpr

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
program dtoepfl;
2+
3+
{$APPTYPE CONSOLE}
4+
{$R *.res}
5+
6+
uses
7+
System.SysUtils,
8+
System.StrUtils,
9+
System.IOUtils,
10+
System.Classes,
11+
System.TimeSpan,
12+
System.Generics.Collections,
13+
System.Diagnostics,
14+
generate.console in '..\..\..\generator\Common\generate.console.pas',
15+
Baseline.Common in '..\..\..\Baseline\Common\Baseline.Common.pas';
16+
17+
type TAlgorithm = (v1, v2, Count);
18+
var algorithm: TAlgorithm;
19+
FormatSettings: TFormatSettings;
20+
const paramPrefix = '--';
21+
paramPrefixShort = '-';
22+
23+
{$REGION 'v1'}
24+
25+
type TStationEntry = record
26+
min, max, sum: Double;
27+
count: Integer;
28+
end;
29+
30+
/// <summary>Simple algorithm, single-thread</summary>
31+
procedure runV1;
32+
var FileStream: TFileStream;
33+
StreamReader: TStreamReader;
34+
CityTemperatures: TDictionary<String, TStationEntry>; //Station, min, avrg, max
35+
Line: String;
36+
Parts: TArray<String>;
37+
entry: TStationEntry;
38+
value: Double;
39+
begin
40+
CityTemperatures := TDictionary<String, TStationEntry>.Create;
41+
42+
try
43+
FileStream := TFileStream.Create(inputFilename, fmOpenRead or fmShareDenyWrite);
44+
try
45+
StreamReader := TStreamReader.Create(FileStream, TEncoding.UTF8);
46+
47+
// Read the first three bytes to check for UTF-8 BOM
48+
// var NumRead := FileStream.Read(BOM, Length(BOM));
49+
// If the file starts with the UTF-8 BOM, continue as is, otherwise reset the stream position.
50+
// if (NumRead <> Length(BOM)) or (BOM[0] <> $EF) or (BOM[1] <> $BB) or (BOM[2] <> $BF) then
51+
// begin
52+
// FileStream.Position := 0; // Reset the position if no BOM or not a UTF-8 BOM
53+
// end;
54+
55+
try
56+
while not StreamReader.EndOfStream do
57+
begin
58+
Line := StreamReader.ReadLine;
59+
Parts := SplitString(Line, ';');
60+
if (TryStrToFloat(Parts[1], value, FormatSettings)) then
61+
begin
62+
if (CityTemperatures.ContainsKey(Parts[0])) then
63+
begin
64+
entry := CityTemperatures[Parts[0]];
65+
66+
if (value < entry.min) then //min
67+
entry.min := value;
68+
69+
entry.sum := (entry.sum + value); //average
70+
entry.count := entry.count + 1;
71+
72+
if (value > entry.max) then //max
73+
entry.max := value;
74+
75+
CityTemperatures[Parts[0]] := entry;
76+
end
77+
else
78+
begin
79+
entry.min := value;
80+
entry.count := 1;
81+
entry.sum := value;
82+
entry.max := value;
83+
CityTemperatures.Add(Parts[0], entry);
84+
end;
85+
end;
86+
end;
87+
88+
89+
//Sorted output
90+
var SortedKeys := TList<String>.Create(CityTemperatures.Keys);
91+
SortedKeys.Sort;
92+
var i := 0;
93+
{$IFDEF DEBUG}
94+
var vStream := TStringStream.Create('', TEncoding.UTF8);
95+
{$ENDIF}
96+
try
97+
for var Key in SortedKeys do
98+
begin
99+
100+
// Windows will mess up the characters when outputting to STDOUT.
101+
// for debug purposes, we'll output it to a file instead.
102+
//https://github.com/gcarreno/1brc-ObjectPascal/blob/main/baseline/Common/baseline.delphi.pas @https://github.com/georges-hatem
103+
{$IFDEF DEBUG}
104+
if (i = 0) then
105+
vStream.WriteString('{');
106+
107+
vStream.WriteString(Format('%s=%.1f/%.1f/%.1f', [key, CityTemperatures[key].min, RoundExDouble(CityTemperatures[key].sum / CityTemperatures[key].count), CityTemperatures[key].max], FormatSettings));
108+
109+
if (i = CityTemperatures.Count-1) then
110+
vStream.WriteString('}' + #10)
111+
else
112+
vStream.WriteString(', ');
113+
114+
{$ELSE}
115+
if (i = 0) then
116+
Write('{');
117+
118+
Write(Format('%s=%.1f/%.1f/%.1f', [key, CityTemperatures[key].min, RoundExDouble(CityTemperatures[key].sum / CityTemperatures[key].count), CityTemperatures[key].max], FormatSettings));
119+
120+
if (i = CityTemperatures.Count-1) then
121+
Write('}' + #10)
122+
else
123+
Write(', ');
124+
{$ENDIF}
125+
126+
Inc(i);
127+
end;
128+
129+
{$IFDEF DEBUG}
130+
vStream.SaveToFile('output.txt');
131+
{$ENDIF}
132+
133+
finally
134+
{$IFDEF DEBUG}
135+
vStream.Free;
136+
{$ENDIF}
137+
end;
138+
139+
finally
140+
StreamReader.Free;
141+
end;
142+
finally
143+
FileStream.Free;
144+
end;
145+
finally
146+
CityTemperatures.Free;
147+
end;
148+
end;
149+
{$ENDREGION}
150+
151+
{$REGION 'v2'}
152+
procedure runV2;
153+
begin
154+
raise Exception.Create('v2 not implemented yet!');
155+
end;
156+
{$ENDREGION}
157+
158+
function TryGetParamValue(index: Int8): String;
159+
begin
160+
try
161+
Result := ParamStr(index + 1).ToLower
162+
except on e:exception do
163+
raise Exception.Create('Invalid parameter for ' + ParamStr(index) + sLineBreak + e.Message);
164+
end;
165+
end;
166+
167+
begin
168+
try
169+
FormatSettings := TFormatSettings.Create;
170+
FormatSettings.DecimalSeparator := '.';
171+
172+
{$REGION 'Application Params'}
173+
var Arg: String;
174+
for var i := 1 to ParamCount do
175+
begin
176+
Arg := ParamStr(i).ToLower;
177+
178+
if (Arg = paramPrefixShort + cShortOptions[2]) or
179+
(Arg = paramPrefix + cLongOptions[2]) then //input-file
180+
inputFilename := TryGetParamValue(i);
181+
182+
if (Arg = paramPrefixShort + 'a') or
183+
(Arg = paramPrefix + 'algorithm') then //algorithm
184+
begin
185+
const v = StringReplace(TryGetParamValue(i), 'v', '', [rfReplaceAll]);
186+
187+
var version: Integer;
188+
if (TryStrToInt(v, version)) then
189+
begin
190+
191+
if (Ord(TAlgorithm.Count) = version) then
192+
begin
193+
case version of
194+
1: algorithm := TAlgorithm.v1;
195+
2: algorithm := TAlgorithm.v2;
196+
end;
197+
end
198+
else
199+
raise Exception.Create('Invalid algorithm version!');
200+
201+
end;
202+
end;
203+
end;
204+
205+
//Check if values are valid
206+
if (inputFilename.IsEmpty) then
207+
raise Exception.Create(Format(rsErrorMessage, [ rsMissingInputFlag ]));
208+
209+
inputFilename := ExpandFileName(inputFilename);
210+
{$ENDREGION}
211+
212+
{$IFDEF DEBUG}
213+
WriteLn('The One Billion Row Challenge in Object Pascal');
214+
WriteLn('Source: https://github.com/dtpfl, https://github.com/gcarreno/1brc-ObjectPascal');
215+
WriteLn;
216+
WriteLn(Format(rsInputFile, [ inputFilename ]));
217+
WriteLn(Format('Algorithm: v%d', [Ord(algorithm)+1]));
218+
WriteLn;
219+
220+
var Stopwatch := TStopwatch.StartNew;
221+
{$ENDIF}
222+
223+
if (algorithm = TAlgorithm.v1) then
224+
runV1
225+
else if (algorithm = TAlgorithm.v2) then
226+
runv2;
227+
228+
{$IFDEF DEBUG}
229+
var Elapsed: TTimeSpan := Stopwatch.Elapsed;
230+
Writeln;
231+
Writeln('Elapsed time: ' + FormatFloat('0.000', Elapsed.TotalSeconds) + ' seconds');
232+
{$ENDIF}
233+
except
234+
on E: Exception do
235+
Writeln(E.ClassName, ': ', E.Message);
236+
end;
237+
end.

0 commit comments

Comments
 (0)