Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions entries/ikelaiah/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ Iwan Kelaiah
* Revision release - Sequential approach. 3-5 mins on my Inspiron 15 7510 laptop, around 3m50s (a little improvement on speed).
* Removed double lookup on dictionaries; removed `.Contains` and used `TryGetValue` instead. This saves approx 60 seconds.

* 1.9
* Revision release - Sequential approach. 3-5 mins on my Inspiron 15 7510 laptop, around 3m8s (a little improvement on speed).
* Use `ShortString` whenever possible. This saves approx 20 seconds.
* Pre-allocate initial size for dictionaries. saves approx 20 seconds.

## License

This project is licensed under the MIT License - see the LICENSE.md file for details
Expand Down
29 changes: 15 additions & 14 deletions entries/ikelaiah/src/weatherstation.pas
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ TStat = record
sum: int64;
cnt: int64;
public
function ToString: string;
function ToString: ShortString;
end;
{Using pointer to TStat saves approx. 30-60 seconds for processing 1 billion rows}
PStat = ^TStat;

type
// Using this dictionary, now approx 4 mins faster than Generics.Collections.TDictionary
TWeatherDictionaryLG = specialize TGHashMapLP<string, PStat>;
TWeatherDictionaryLG = specialize TGHashMapLP<ShortString, PStat>;

type
// a type for storing valid lookup temperature
TValidTemperatureDictionary = specialize TGHashMapLP<string, int64>;
TValidTemperatureDictionary = specialize TGHashMapLP<ShortString, int64>;

type
// Create a class to encapsulate the temperature observations of each weather station.
Expand All @@ -51,8 +51,8 @@ TWeatherStation = class
lookupStrFloatToIntList: TValidTemperatureDictionary;
procedure CreateLookupTemp;
procedure ReadMeasurements;
procedure ParseStationAndTemp(const line: string);
procedure AddCityTemperatureLG(const cityName: string; const newTemp: int64);
procedure ParseStationAndTemp(const line: ShortString);
procedure AddCityTemperatureLG(const cityName: ShortString; const newTemp: int64);
procedure SortWeatherStationAndStats;
procedure PrintSortedWeatherStationAndStats;
public
Expand Down Expand Up @@ -91,7 +91,7 @@ function CustomTStringListComparer(AList: TStringList;
end;

// Remove dots from a string
function RemoveDots(const line: string): string;
function RemoveDots(const line: ShortString): ShortString;
var
index: integer;
begin
Expand All @@ -103,7 +103,7 @@ function RemoveDots(const line: string): string;
end;
end;

function TStat.ToString: string;
function TStat.ToString: ShortString;
var
minR, meanR, maxR: double; // Store the rounded values prior saving to TStringList.
begin
Expand All @@ -120,15 +120,18 @@ constructor TWeatherStation.Create(const filename: string);
fname := filename;
// Create a lookup
self.lookupStrFloatToIntList := TValidTemperatureDictionary.Create;
// Set expected capacity - saves 10 seconds.
self.lookupStrFloatToIntList.EnsureCapacity(44691);
// Create a dictionary
weatherDictionary := TWeatherDictionaryLG.Create;
weatherDictionary.EnsureCapacity(44691);
// Create a TStringList for sorting
weatherStationList := TStringList.Create;
end;

destructor TWeatherStation.Destroy;
var
stationName: string;
stationName: ShortString;
begin

// Free the lookup dictionary
Expand All @@ -150,7 +153,6 @@ procedure TWeatherStation.CreateLookupTemp;
startTemp: int64 = -1000;
finishTemp: int64 = 1000;
currentTemp: int64;
numStr: string;
begin

currentTemp := startTemp;
Expand Down Expand Up @@ -203,7 +205,7 @@ procedure TWeatherStation.PrintSortedWeatherStationAndStats;

procedure TWeatherStation.SortWeatherStationAndStats;
var
wsKey: string;
wsKey: ShortString;
begin

{$IFDEF DEBUG}
Expand Down Expand Up @@ -232,7 +234,7 @@ procedure TWeatherStation.SortWeatherStationAndStats;
{$ENDIF DEBUG}
end;

procedure TWeatherStation.AddCityTemperatureLG(const cityName: string;
procedure TWeatherStation.AddCityTemperatureLG(const cityName: ShortString;
const newTemp: int64);
var
stat: PStat;
Expand Down Expand Up @@ -285,11 +287,10 @@ procedure TWeatherStation.AddCityTemperatureLG(const cityName: string;
end;
end;

procedure TWeatherStation.ParseStationAndTemp(const line: string);
procedure TWeatherStation.ParseStationAndTemp(const line: ShortString);
var
delimiterPos: integer;
parsedStation, strFloatTemp: string;
results: array of string;
parsedStation, strFloatTemp: ShortString;
parsedTemp, valCode: int64;
begin
// Get position of the delimiter
Expand Down