Skip to content

Commit a9ae738

Browse files
committed
New - Initial commit.
1 parent 6a83328 commit a9ae738

6 files changed

Lines changed: 596 additions & 0 deletions

File tree

entries/ikelaiah/LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2024 ikelaiah
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

entries/ikelaiah/README.md

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