Skip to content

Commit 08853ce

Browse files
authored
Merge pull request #18 from ikelaiah/ikelaiah-entry
ikelaiah's entry to the 1 BRC. New - Initial commit.
2 parents 3169442 + 6374940 commit 08853ce

File tree

5 files changed

+594
-0
lines changed

5 files changed

+594
-0
lines changed

entries/ikelaiah/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Iwan Kelaiah
2+
3+
An Entry to the One Billion Row Challenge in Object Pascal.
4+
5+
The approach I implemented here is simplistic.
6+
7+
- Sequentially read the measurement file.
8+
- Populate a `TDictionary` with station names, min, max, count and sum; without storing all the temperature measurements.
9+
- Use a custom comparer to sort the station and temperature statistics in a `TStringList`.
10+
- Display the sorted measurements using a simple for loop.
11+
12+
## Getting Started
13+
14+
### Dependencies
15+
16+
* None. Only latest Free Pascal Compiler and Lazarus. You can get these easily by using [`https://github.com/LongDirtyAnimAlf/fpcupdeluxe/releases`](https://github.com/LongDirtyAnimAlf/fpcupdeluxe/releases).
17+
18+
### Compiling
19+
20+
* Open `OneBRC.lpi` using Lazarus.
21+
* Hit `Ctrl + F9` to compile.
22+
23+
### Running the executable
24+
25+
```bash
26+
$ ./OneBRC.exe -i your_measurements.txt
27+
```
28+
29+
To time the execution, do the following.
30+
31+
```bash
32+
$ time ./OneBRC.exe -i your_measurements.txt
33+
```
34+
35+
## Help
36+
37+
To see flags, use `-h`.
38+
39+
```
40+
$ ./OneBRC.exe -h
41+
OneBRC -- An entry to the One Billion Row Challenge for Object Pascal
42+
43+
Usage: OneBRC [-h] [-v] [-i input_file]
44+
45+
-h | --help Show this help screen
46+
-v | --version Show the version number
47+
-i | --input-file <filename> Input text file to process.
48+
Each row is one temperature measurement in the format <string: station name>;<double: measurement>
49+
```
50+
51+
Use `-v` to check version.
52+
53+
```bash
54+
$ ./OneBRC.exe -v
55+
OneBRC version 1.0
56+
```
57+
58+
## Authors
59+
60+
Iwan Kelaiah
61+
[ikelaiah](https://github.com/ikelaiah)
62+
63+
## Version History
64+
65+
* 1.0
66+
* Initial Release - Sequential approach.
67+
68+
## License
69+
70+
This project is licensed under the MIT License - see the LICENSE.md file for details
71+
72+
## Acknowledgments
73+
74+
Inspiration, code snippets, etc.
75+
76+
1. The FPC team, Lazarus team, fpcupdeluxe team, and other contributors.
77+
- For providing a usable programming language and a usable ecosystem.
78+
2. Gustavo 'Gus' Carreno.
79+
- For making this happen.
80+
- Borrowed Gus' approach to use `TCustomApplication` and using `unit`s properly
81+
to make main code more readable.
82+
- Borrowed and modified Gus' `WriteHelp` from the `baseline.lpr`.
83+
3. Székely Balázs.
84+
- Now I know what `Single` data type is!
85+
- I borrowed the custom `TStringList` comparer from the `baseline` program.
86+
4. Shraddha Agrawal - https://www.bytesizego.com/blog/one-billion-row-challenge-go.
87+
- The advice for not storing measurements for each station in a data structure.
88+
5. Arman Hajisafi - https://arman-hs.github.io
89+
- Encouragements and inspirations.

entries/ikelaiah/src/OneBRC.lpi

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CONFIG>
3+
<ProjectOptions>
4+
<Version Value="12"/>
5+
<PathDelim Value="\"/>
6+
<General>
7+
<Flags>
8+
<MainUnitHasCreateFormStatements Value="False"/>
9+
<MainUnitHasTitleStatement Value="False"/>
10+
<MainUnitHasScaledStatement Value="False"/>
11+
</Flags>
12+
<SessionStorage Value="InProjectDir"/>
13+
<Title Value="OneBRC"/>
14+
<UseAppBundle Value="False"/>
15+
<ResourceType Value="res"/>
16+
</General>
17+
<BuildModes>
18+
<Item Name="Default" Default="True"/>
19+
</BuildModes>
20+
<PublishOptions>
21+
<Version Value="2"/>
22+
<UseFileFilters Value="True"/>
23+
</PublishOptions>
24+
<RunParams>
25+
<FormatVersion Value="2"/>
26+
</RunParams>
27+
<Units>
28+
<Unit>
29+
<Filename Value="OneBRC.lpr"/>
30+
<IsPartOfProject Value="True"/>
31+
</Unit>
32+
<Unit>
33+
<Filename Value="stopwatch.pas"/>
34+
<IsPartOfProject Value="True"/>
35+
<UnitName Value="Stopwatch"/>
36+
</Unit>
37+
<Unit>
38+
<Filename Value="weatherstation.pas"/>
39+
<IsPartOfProject Value="True"/>
40+
<UnitName Value="WeatherStation"/>
41+
</Unit>
42+
</Units>
43+
</ProjectOptions>
44+
<CompilerOptions>
45+
<Version Value="11"/>
46+
<PathDelim Value="\"/>
47+
<Target>
48+
<Filename Value="OneBRC"/>
49+
</Target>
50+
<SearchPaths>
51+
<IncludeFiles Value="$(ProjOutDir)"/>
52+
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
53+
</SearchPaths>
54+
<CodeGeneration>
55+
<SmartLinkUnit Value="True"/>
56+
<RelocatableUnit Value="True"/>
57+
<Optimizations>
58+
<OptimizationLevel Value="2"/>
59+
</Optimizations>
60+
</CodeGeneration>
61+
<Linking>
62+
<LinkSmart Value="True"/>
63+
</Linking>
64+
</CompilerOptions>
65+
<Debugging>
66+
<Exceptions>
67+
<Item>
68+
<Name Value="EAbort"/>
69+
</Item>
70+
<Item>
71+
<Name Value="ECodetoolError"/>
72+
</Item>
73+
<Item>
74+
<Name Value="EFOpenError"/>
75+
</Item>
76+
</Exceptions>
77+
</Debugging>
78+
</CONFIG>

entries/ikelaiah/src/OneBRC.lpr

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
program OneBRC;
2+
3+
{
4+
==Credits==
5+
6+
1. The FPC team, Lazarus team, fpcupdeluxe team, and other contributors.
7+
- For providing a usable programming language and a usable ecosystem.
8+
2. Gustavo 'Gus' Carreno.
9+
- For making this happen.
10+
- Borrowed Gus' approach to use `TCustomApplication` and using `unit`s properly
11+
to make main code more readable.
12+
- Borrowed and modified Gus' `WriteHelp` from the `baseline.lpr`.
13+
3. Székely Balázs.
14+
- Now I know what `Single` data type is!
15+
- I borrowed the custom `TStringList` comparer from the `baseline` program.
16+
4. Shraddha Agrawal - https://www.bytesizego.com/blog/one-billion-row-challenge-go.
17+
- The advice for not storing measurements for each station in a data structure.
18+
5. Arman Hajisafi - https://arman-hs.github.io
19+
- Encouragements and inspirations.
20+
}
21+
22+
{$mode objfpc}{$H+}{$J-}{$modeSwitch advancedRecords}
23+
{$codepage utf8}
24+
//{$DEFINE DEBUG}
25+
26+
uses
27+
{$IFDEF UNIX}
28+
cthreads,
29+
{$ENDIF}
30+
Classes,
31+
SysUtils,
32+
CustApp,
33+
WeatherStation;
34+
35+
const
36+
version = '1.0';
37+
38+
type
39+
40+
{ TOneBRC }
41+
42+
TOneBRC = class(TCustomApplication)
43+
protected
44+
procedure DoRun; override;
45+
public
46+
constructor Create(TheOwner: TComponent); override;
47+
destructor Destroy; override;
48+
procedure WriteHelp; virtual;
49+
end;
50+
51+
{ TOneBRC }
52+
53+
procedure TOneBRC.DoRun;
54+
var
55+
ErrorMsg: string;
56+
filename: string = '';
57+
begin
58+
// quick check parameters
59+
ErrorMsg := CheckOptions('hvi:', ['help', 'version', 'input:']);
60+
if ErrorMsg <> '' then
61+
begin
62+
// Commented out the default ShowException as the generated text is not user friendly.
63+
// ShowException(Exception.Create(ErrorMsg));
64+
WriteLn('Error: ', ErrorMsg);
65+
WriteHelp;
66+
Terminate;
67+
Exit;
68+
end;
69+
70+
// Parse h
71+
if HasOption('h', 'help') then
72+
begin
73+
WriteHelp;
74+
Terminate;
75+
Exit;
76+
end;
77+
78+
// Parse v
79+
if HasOption('v', 'version') then
80+
begin
81+
WriteLn('OneBRC version ', version);
82+
Terminate;
83+
Exit;
84+
end;
85+
86+
// Parse i
87+
if HasOption('i', 'input') then
88+
begin
89+
filename := GetOptionValue('i', 'input');
90+
end;
91+
92+
if (length(filename) < 4) then
93+
begin
94+
WriteLn('Input file seems invalid.');
95+
WriteHelp;
96+
Terminate;
97+
Exit;
98+
end;
99+
100+
// Start the main algorithm
101+
WeatherStation.ProcessTempMeasurements(filename);
102+
103+
// stop program loop
104+
Terminate;
105+
end;
106+
107+
constructor TOneBRC.Create(TheOwner: TComponent);
108+
begin
109+
inherited Create(TheOwner);
110+
StopOnException := True;
111+
end;
112+
113+
destructor TOneBRC.Destroy;
114+
begin
115+
inherited Destroy;
116+
end;
117+
118+
procedure TOneBRC.WriteHelp;
119+
begin
120+
WriteLn('OneBRC -- An entry to the One Billion Row Challenge for Object Pascal');
121+
WriteLn;
122+
WriteLn('Usage: OneBRC [-h] [-v] [-i input_file]');
123+
WriteLn;
124+
WriteLn(' -h | --help Show this help screen');
125+
WriteLn(' -v | --version Show the version number');
126+
WriteLn(' -i | --input-file <filename> Input text file to process.');
127+
WriteLn(' Each row is one temperature measurement in the format <string: station name>;<double: measurement>');
128+
end;
129+
130+
var
131+
Application: TOneBRC;
132+
begin
133+
Application := TOneBRC.Create(nil);
134+
Application.Title := 'OneBRC';
135+
Application.Run;
136+
Application.Free;
137+
end.

entries/ikelaiah/src/stopwatch.pas

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
unit Stopwatch;
2+
3+
{$mode objfpc}{$H+}{$J-}{$modeSwitch advancedRecords}
4+
5+
interface
6+
7+
uses
8+
Classes, SysUtils;
9+
10+
procedure StartTimer;
11+
procedure StopTimer;
12+
procedure ResetTimer;
13+
procedure DisplayTimer;
14+
15+
implementation
16+
17+
var
18+
startTime: QWord = 0;
19+
endTime: QWord = 0;
20+
elapsedMilliseconds: QWord = 0;
21+
hours, minutes, seconds, milliseconds: word;
22+
23+
procedure StartTimer;
24+
begin
25+
startTime := GetTickCount64;
26+
end;
27+
28+
procedure StopTimer;
29+
begin
30+
endTime := GetTickCount64;
31+
end;
32+
33+
procedure ResetTimer;
34+
begin
35+
startTime := 0;
36+
endTime := 0;
37+
elapsedMilliseconds := 0;
38+
end;
39+
40+
procedure DisplayTimer;
41+
begin
42+
43+
// Elapsed milliseconds
44+
elapsedMilliseconds := endTime - startTime;
45+
46+
// Convert milliseconds to hours, minutes, seconds, and milliseconds
47+
hours := elapsedMilliseconds div 3600000;
48+
elapsedMilliseconds := elapsedMilliseconds mod 3600000;
49+
50+
minutes := elapsedMilliseconds div 60000;
51+
elapsedMilliseconds := elapsedMilliseconds mod 60000;
52+
53+
seconds := elapsedMilliseconds div 1000;
54+
milliseconds := elapsedMilliseconds mod 1000;
55+
56+
WriteLn;
57+
WriteLn('------------------------------');
58+
WriteLn('Elapsed time: ', hours, ' hours ', minutes, ' minutes ',
59+
seconds, ' seconds ', milliseconds, ' milliseconds');
60+
//WriteLn('Elapsed time: ', (endTime - startTime), ' ms');
61+
end;
62+
63+
64+
end.

0 commit comments

Comments
 (0)