diff --git a/entries/ikelaiah/README.md b/entries/ikelaiah/README.md index fa2b0f6..db4c9bc 100644 --- a/entries/ikelaiah/README.md +++ b/entries/ikelaiah/README.md @@ -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 diff --git a/entries/ikelaiah/src/weatherstation.pas b/entries/ikelaiah/src/weatherstation.pas index bfcf397..7873610 100644 --- a/entries/ikelaiah/src/weatherstation.pas +++ b/entries/ikelaiah/src/weatherstation.pas @@ -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; + TWeatherDictionaryLG = specialize TGHashMapLP; type // a type for storing valid lookup temperature - TValidTemperatureDictionary = specialize TGHashMapLP; + TValidTemperatureDictionary = specialize TGHashMapLP; type // Create a class to encapsulate the temperature observations of each weather station. @@ -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 @@ -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 @@ -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 @@ -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 @@ -150,7 +153,6 @@ procedure TWeatherStation.CreateLookupTemp; startTemp: int64 = -1000; finishTemp: int64 = 1000; currentTemp: int64; - numStr: string; begin currentTemp := startTemp; @@ -203,7 +205,7 @@ procedure TWeatherStation.PrintSortedWeatherStationAndStats; procedure TWeatherStation.SortWeatherStationAndStats; var - wsKey: string; + wsKey: ShortString; begin {$IFDEF DEBUG} @@ -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; @@ -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