Skip to content

Commit 641ebb3

Browse files
committed
Update - Rev 09.
1 parent 7770fea commit 641ebb3

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

entries/ikelaiah/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ Iwan Kelaiah
131131
* Revision release - Sequential approach. 3-5 mins on my Inspiron 15 7510 laptop, around 3m50s (a little improvement on speed).
132132
* Removed double lookup on dictionaries; removed `.Contains` and used `TryGetValue` instead. This saves approx 60 seconds.
133133

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

136141
This project is licensed under the MIT License - see the LICENSE.md file for details

entries/ikelaiah/src/weatherstation.pas

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3636
type
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

4040
type
4141
// a type for storing valid lookup temperature
42-
TValidTemperatureDictionary = specialize TGHashMapLP<string, int64>;
42+
TValidTemperatureDictionary = specialize TGHashMapLP<ShortString, int64>;
4343

4444
type
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;
9191
end;
9292

9393
// Remove dots from a string
94-
function RemoveDots(const line: string): string;
94+
function RemoveDots(const line: ShortString): ShortString;
9595
var
9696
index: integer;
9797
begin
@@ -103,7 +103,7 @@ function RemoveDots(const line: string): string;
103103
end;
104104
end;
105105

106-
function TStat.ToString: string;
106+
function TStat.ToString: ShortString;
107107
var
108108
minR, meanR, maxR: double; // Store the rounded values prior saving to TStringList.
109109
begin
@@ -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;
127130
end;
128131

129132
destructor TWeatherStation.Destroy;
130133
var
131-
stationName: string;
134+
stationName: ShortString;
132135
begin
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;
154156
begin
155157

156158
currentTemp := startTemp;
@@ -203,7 +205,7 @@ procedure TWeatherStation.PrintSortedWeatherStationAndStats;
203205

204206
procedure TWeatherStation.SortWeatherStationAndStats;
205207
var
206-
wsKey: string;
208+
wsKey: ShortString;
207209
begin
208210

209211
{$IFDEF DEBUG}
@@ -232,7 +234,7 @@ procedure TWeatherStation.SortWeatherStationAndStats;
232234
{$ENDIF DEBUG}
233235
end;
234236

235-
procedure TWeatherStation.AddCityTemperatureLG(const cityName: string;
237+
procedure TWeatherStation.AddCityTemperatureLG(const cityName: ShortString;
236238
const newTemp: int64);
237239
var
238240
stat: PStat;
@@ -285,11 +287,10 @@ procedure TWeatherStation.AddCityTemperatureLG(const cityName: string;
285287
end;
286288
end;
287289

288-
procedure TWeatherStation.ParseStationAndTemp(const line: string);
290+
procedure TWeatherStation.ParseStationAndTemp(const line: ShortString);
289291
var
290292
delimiterPos: integer;
291-
parsedStation, strFloatTemp: string;
292-
results: array of string;
293+
parsedStation, strFloatTemp: ShortString;
293294
parsedTemp, valCode: int64;
294295
begin
295296
// Get position of the delimiter

0 commit comments

Comments
 (0)