@@ -28,18 +28,18 @@ TStat = record
2828 sum: int64;
2929 cnt: int64;
3030 public
31- function ToString : string ;
31+ function ToString : ShortString ;
3232 end ;
3333 { Using pointer to TStat saves approx. 30-60 seconds for processing 1 billion rows}
3434 PStat = ^TStat;
3535
3636type
3737 // Using this dictionary, now approx 4 mins faster than Generics.Collections.TDictionary
38- TWeatherDictionaryLG = specialize TGHashMapLP<string , PStat>;
38+ TWeatherDictionaryLG = specialize TGHashMapLP<ShortString , PStat>;
3939
4040type
4141 // a type for storing valid lookup temperature
42- TValidTemperatureDictionary = specialize TGHashMapLP<string , int64>;
42+ TValidTemperatureDictionary = specialize TGHashMapLP<ShortString , int64>;
4343
4444type
4545 // Create a class to encapsulate the temperature observations of each weather station.
@@ -51,8 +51,8 @@ TWeatherStation = class
5151 lookupStrFloatToIntList: TValidTemperatureDictionary;
5252 procedure CreateLookupTemp ;
5353 procedure ReadMeasurements ;
54- procedure ParseStationAndTemp (const line: string );
55- procedure AddCityTemperatureLG (const cityName: string ; const newTemp: int64);
54+ procedure ParseStationAndTemp (const line: ShortString );
55+ procedure AddCityTemperatureLG (const cityName: ShortString ; const newTemp: int64);
5656 procedure SortWeatherStationAndStats ;
5757 procedure PrintSortedWeatherStationAndStats ;
5858 public
@@ -91,7 +91,7 @@ function CustomTStringListComparer(AList: TStringList;
9191end ;
9292
9393// Remove dots from a string
94- function RemoveDots (const line: string ): string ;
94+ function RemoveDots (const line: ShortString ): ShortString ;
9595var
9696 index: integer;
9797begin
@@ -103,7 +103,7 @@ function RemoveDots(const line: string): string;
103103 end ;
104104end ;
105105
106- function TStat.ToString : string ;
106+ function TStat.ToString : ShortString ;
107107var
108108 minR, meanR, maxR: double; // Store the rounded values prior saving to TStringList.
109109begin
@@ -120,15 +120,18 @@ constructor TWeatherStation.Create(const filename: string);
120120 fname := filename;
121121 // Create a lookup
122122 self.lookupStrFloatToIntList := TValidTemperatureDictionary.Create;
123+ // Set expected capacity - saves 10 seconds.
124+ self.lookupStrFloatToIntList.EnsureCapacity(44691 );
123125 // Create a dictionary
124126 weatherDictionary := TWeatherDictionaryLG.Create;
127+ weatherDictionary.EnsureCapacity(44691 );
125128 // Create a TStringList for sorting
126129 weatherStationList := TStringList.Create;
127130end ;
128131
129132destructor TWeatherStation.Destroy;
130133var
131- stationName: string ;
134+ stationName: ShortString ;
132135begin
133136
134137 // Free the lookup dictionary
@@ -150,7 +153,6 @@ procedure TWeatherStation.CreateLookupTemp;
150153 startTemp: int64 = -1000 ;
151154 finishTemp: int64 = 1000 ;
152155 currentTemp: int64;
153- numStr: string;
154156begin
155157
156158 currentTemp := startTemp;
@@ -203,7 +205,7 @@ procedure TWeatherStation.PrintSortedWeatherStationAndStats;
203205
204206procedure TWeatherStation.SortWeatherStationAndStats ;
205207var
206- wsKey: string ;
208+ wsKey: ShortString ;
207209begin
208210
209211 { $IFDEF DEBUG}
@@ -232,7 +234,7 @@ procedure TWeatherStation.SortWeatherStationAndStats;
232234 { $ENDIF DEBUG}
233235end ;
234236
235- procedure TWeatherStation.AddCityTemperatureLG (const cityName: string ;
237+ procedure TWeatherStation.AddCityTemperatureLG (const cityName: ShortString ;
236238 const newTemp: int64);
237239var
238240 stat: PStat;
@@ -285,11 +287,10 @@ procedure TWeatherStation.AddCityTemperatureLG(const cityName: string;
285287 end ;
286288end ;
287289
288- procedure TWeatherStation.ParseStationAndTemp (const line: string );
290+ procedure TWeatherStation.ParseStationAndTemp (const line: ShortString );
289291var
290292 delimiterPos: integer;
291- parsedStation, strFloatTemp: string;
292- results: array of string;
293+ parsedStation, strFloatTemp: ShortString;
293294 parsedTemp, valCode: int64;
294295begin
295296 // Get position of the delimiter
0 commit comments