@@ -37,6 +37,14 @@ TGenerator = class(TObject)
3737
3838implementation
3939
40+ uses
41+ Math,
42+ bufstream
43+ ;
44+
45+ const
46+ batchPercent = 10 ;
47+
4048{ TGenerator }
4149
4250constructor TGenerator.Create(
@@ -115,10 +123,11 @@ function TGenerator.GenerateProgressBar(APosition, AMax, ALength: Int64
115123
116124procedure TGenerator.Generate ;
117125var
118- index: Int64;
126+ index, progressBatch : Int64;
119127 stationId: Int64;
120128 randomTemp: Double;
121- outputStream: TFileStream;
129+ outputFileStream: TFileStream;
130+ outputBufWriter: TWriteBufStream;
122131 line: String;
123132begin
124133 // Randomize sets this variable depending on the current time
@@ -128,23 +137,37 @@ procedure TGenerator.Generate;
128137 // Build list of station names
129138 BuildStationNames;
130139
131- outputStream:= TFileStream.Create(FOutPutFile, fmCreate);
140+ outputFileStream:= TFileStream.Create(FOutPutFile, fmCreate);
141+
142+ progressBatch:= floor(FLineCount * (batchPercent / 100 ));
143+
132144 try
133- // Generate the file
134- for index:= 1 to FLineCount do
135- begin
136- stationId:= Random(FStationNames.Count);
137- randomTemp:= Random * (2 * cHottestTemp) - cHottestTemp;
138- line:= Format(' %s;%s' #13 #10 , [
139- FStationNames[stationId],
140- FormatFloat(' #0.0' , randomTemp)
141- ]);
142- // Write(line);
143- outputStream.WriteBuffer(line[1 ], Length(line));
144- Write(GenerateProgressBar(index, FLineCount, 50 ), #13 );
145+ outputBufWriter:= TWriteBufStream.Create(outputFileStream, 20 *1024 *1024 );
146+ try
147+ Write(GenerateProgressBar(1 , FLineCount, 50 ), #13 );
148+ // Generate the file
149+ for index:= 1 to FLineCount do
150+ begin
151+ stationId:= Random(FStationNames.Count);
152+ randomTemp:= Random * (2 * cHottestTemp) - cHottestTemp;
153+ line:= Format(' %s;%s' #13 #10 , [
154+ FStationNames[stationId],
155+ FormatFloat(' #0.0' , randomTemp)
156+ ]);
157+ // Write(line);
158+ outputFileStream.WriteBuffer(line[1 ], Length(line));
159+ Dec(progressBatch);
160+ if progressBatch = 0 then
161+ begin
162+ Write(GenerateProgressBar(index, FLineCount, 50 ), #13 );
163+ progressBatch:= floor(FLineCount * (batchPercent / 100 ));
164+ end ;
165+ end ;
166+ finally
167+ outputBufWriter.Free;
145168 end ;
146169 finally
147- outputStream .Free;
170+ outputFileStream .Free;
148171 end ;
149172 WriteLn;
150173end ;
0 commit comments