Skip to content

Commit bb50add

Browse files
authored
Merge pull request #5 from paweld/gus
a few more optimizations
2 parents 5593525 + 426166e commit bb50add

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

Diff for: generator/Common/generate.common.pas

+29-13
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ procedure TGenerator.Generate;
153153
outputBufWriter: TWriteBufStream;
154154
line, randomTempFinal: String;
155155
stationArray, temperatureArray: TStringArray;
156-
i, stationsCount, temperaturesCount: Integer;
156+
LenStationArray, LenTemperatureArray: Array of Integer;
157+
i, count, len, stationsCount, temperaturesCount: Integer;
157158
start: TDateTime;
158159
begin
159160
// Randomize sets this variable depending on the current time
@@ -171,12 +172,18 @@ procedure TGenerator.Generate;
171172
//based on code @domasz from lazarus forum, github: PascalVault
172173
stationsCount := FStationNames.Count;
173174
SetLength(stationArray, stationsCount);
175+
SetLength(LenStationArray, stationsCount);
174176
for i := 0 to stationsCount - 1 do
175-
stationArray[i] := FStationNames[i];
177+
begin
178+
stationArray[i] := FStationNames[i] + ';';
179+
LenStationArray[i] := Length(stationArray[i]);
180+
end;
176181

177182
temperaturesCount := 1999;
178183
SetLength(temperatureArray, temperaturesCount);
179-
temperatureArray[0] := '0.0';
184+
SetLength(LenTemperatureArray, temperaturesCount);
185+
temperatureArray[0] := '0.0' + #13#10;
186+
LenTemperatureArray[0] := Length(temperatureArray[0]);
180187
for i := 1 to 999 do
181188
begin
182189
randomTempStr := IntToStr(i);
@@ -186,12 +193,16 @@ procedure TGenerator.Generate;
186193
3: randomTempFinal := randomTempStr[1] + randomTempStr[2] + '.' + randomTempStr[3];
187194
4: randomTempFinal := randomTempStr[1] + randomTempStr[2] + randomTempStr[3] + '.' + randomTempStr[4];
188195
end;
189-
temperatureArray[i * 2 - 1] := randomTempFinal;
190-
temperatureArray[i * 2] := '-' + randomTempFinal;
196+
temperatureArray[i * 2 - 1] := randomTempFinal + #13#10;
197+
LenTemperatureArray[i * 2 - 1] := Length(temperatureArray[i * 2 - 1]);
198+
temperatureArray[i * 2] := '-' + randomTempFinal + #13#10;
199+
LenTemperatureArray[i * 2] := LenTemperatureArray[i * 2 - 1] + 1;
191200
end;
192201
//
193202

194-
line := '';
203+
count := 0;
204+
len := 0;
205+
SetLength(line, 1024 * 1024 * 20);
195206

196207
try
197208
//outputBufWriter:= TWriteBufStream.Create(outputFileStream, 4*1024);
@@ -205,12 +216,17 @@ procedure TGenerator.Generate;
205216
// This is all paweld magic:
206217
// From here
207218
randomTemp:= Random(temperaturesCount);
208-
line := line + stationArray[stationId] + ';' + temperatureArray[randomTemp] + #13#10;
209-
//Write(line);
210-
if index mod 10000 = 0 then
219+
Move(stationArray[stationId][1], line[len + 1], LenStationArray[stationId]);
220+
Inc(len, LenStationArray[stationId]);
221+
Move(temperatureArray[randomTemp][1], line[len + 1], LenTemperatureArray[randomTemp]);
222+
Inc(len, LenTemperatureArray[randomTemp]);
223+
224+
Inc(count);
225+
if count = 10000 then
211226
begin
212-
outputBufWriter.WriteBuffer(line[1], Length(line));
213-
line := '';
227+
outputBufWriter.WriteBuffer(line[1], len);
228+
count := 0;
229+
len := 0;
214230
end;
215231
// To here
216232
Dec(progressBatch);
@@ -226,9 +242,9 @@ procedure TGenerator.Generate;
226242
progressBatch:= floor(FLineCount * (batchPercent / 100));
227243
end;
228244
end;
229-
if line <> '' then
245+
if count > 0 then
230246
begin
231-
outputBufWriter.WriteBuffer(line[1], Length(line));
247+
outputBufWriter.WriteBuffer(line[1], len);
232248
end;
233249
finally
234250
outputBufWriter.Free;

0 commit comments

Comments
 (0)