Skip to content

Commit db565bd

Browse files
68412542+EagleAglow@users.noreply.github.com68412542+EagleAglow@users.noreply.github.com
authored andcommitted
Version 2.1
Minor speed tweaks
1 parent 3b0f544 commit db565bd

File tree

4 files changed

+41
-46
lines changed

4 files changed

+41
-46
lines changed

entries/bfire/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ Finally, the TStringList is sorted and used to output sorted data.
5555
- Version 1.0: first working version, based on TStringList.
5656
- Version 1.1: modified rounding to new baseline.
5757
- Version 2.0: use hashing, sort later.
58+
- Version 2.1: minor speed tweaks.

entries/bfire/src/ProcessByHashUnit.pas

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,18 @@ TStationForSort = class of TStationForSortClass;
6060
// size entry_temp_array to hold -9999
6161
entry_name_array: array [0 .. 255] of Byte; // will hold place name
6262
entry_temp_array: array [0 .. 15] of Byte; // will hold place temperature
63+
entry_name_array_length: Integer;
64+
entry_temp_array_length: Integer;
6365

6466
// prepare empty arrays to initialize before each entry read
6567
empty_entry_name_array: array [0 .. 255] of Byte;
6668
empty_entry_temp_array: array [0 .. 15] of Byte;
6769

6870
procedure FileToArrays(inFile: String; UseStdOut: Boolean);
69-
procedure HashAndSave(K: Integer; J: Integer);
70-
function BytesToTemp(count: Integer): Integer;
71-
function StaticBytesToString(K: Integer): String;
72-
function BytesToIndexHash(count: Integer): Integer;
71+
procedure HashAndSave;
72+
function BytesToTemp: Integer;
73+
function StaticBytesToString: String;
74+
function BytesToIndexHash: Integer;
7375
procedure SortArrays;
7476
procedure ArrayToFile(outFile: String; UseStdOut: Boolean);
7577

@@ -144,8 +146,7 @@ function MeanFixup(total: Integer; count: Integer): String; inline;
144146
MeanFixup := temp; // was temp;
145147
end;
146148

147-
function BytesToTemp(count: Integer): Integer; inline;
148-
// entry_temp_array is global, only pass length
149+
function BytesToTemp: Integer;
149150
// convert entry_chars_temp to signed integer, '0' is ascii 48 decimal
150151
// note: we always have at least two bytes
151152
// temperatures range from -99.9 to 99.8
@@ -158,9 +159,9 @@ function BytesToTemp(count: Integer): Integer; inline;
158159
// BTT := -(entry_temp_array[count -1] -48) - 10 * (entry_temp_array[count -2] - 48);
159160
// BTT := 48 - entry_temp_array[count -1] - 10 * (entry_temp_array[count -2] - 48);
160161
// BTT := 48 + 480 - entry_temp_array[count -1] - 10 * (entry_temp_array[count -2]);
161-
BTT := 528 - entry_temp_array[count - 1] - 10 *
162-
(entry_temp_array[count - 2]);
163-
if count = 4 then // do one more digit
162+
BTT := 528 - entry_temp_array[entry_temp_array_length - 1] - 10 *
163+
(entry_temp_array[entry_temp_array_length - 2]);
164+
if entry_temp_array_length = 4 then // do one more digit
164165
begin
165166
BTT := BTT - 100 * (entry_temp_array[1] - 48);
166167
end;
@@ -170,9 +171,9 @@ function BytesToTemp(count: Integer): Integer; inline;
170171
// BTT := (entry_temp_array[count -1] -48) + 10 * (entry_temp_array[count -2] - 48);
171172
// BTT := entry_temp_array[count -1] -48 + 10 * (entry_temp_array[count -2] - 48);
172173
// BTT := entry_temp_array[count -1] -48 - 480 + 10 * (entry_temp_array[count -2]);
173-
BTT := -528 + (entry_temp_array[count - 1]) + 10 *
174-
(entry_temp_array[count - 2]);
175-
if count = 3 then // do one more digit
174+
BTT := -528 + (entry_temp_array[entry_temp_array_length - 1]) + 10 *
175+
(entry_temp_array[entry_temp_array_length - 2]);
176+
if entry_temp_array_length = 3 then // do one more digit
176177
begin
177178
BTT := BTT + 100 * (entry_temp_array[0] - 48);
178179
end;
@@ -186,8 +187,7 @@ function BytesToTemp(count: Integer): Integer; inline;
186187
(* ------------------------------------------------------------------ *)
187188
(* function TDPJWHash *)
188189
(* ****************************************************************** *)
189-
function BytesToIndexHash(count: Integer): Integer; inline;
190-
// entry_name_array is global, only pass length
190+
function BytesToIndexHash;
191191
// convert entry_chars to hash
192192
// use Prime: Integer = 75013;
193193
var
@@ -196,7 +196,7 @@ function BytesToIndexHash(count: Integer): Integer; inline;
196196
Hash: Integer;
197197
begin
198198
Hash := 0;
199-
for i := 0 to count - 1 do
199+
for i := 0 to entry_name_array_length - 1 do
200200
begin
201201
Hash := (Hash shl 4) + entry_name_array[i];
202202
G := Hash and $F0000000;
@@ -233,23 +233,21 @@ procedure InitializeThings; inline;
233233
StationsForSort.Sorted := False; // sort later
234234
end;
235235

236-
function StaticBytesToString(K: Integer): String; inline;
237-
// entry_name_array is global, only pass length
236+
function StaticBytesToString;
238237
var
239238
ugly: TArray<Byte>; // prefer not to use TArray, but for now...
240239
i: Integer;
241240
begin
242-
SetLength(ugly, K);
243-
for i := 0 to K - 1 do
241+
SetLength(ugly, entry_name_array_length);
242+
for i := 0 to entry_name_array_length - 1 do
244243
begin
245244
ugly[i] := entry_name_array[i];
246245
end;
247246
StaticBytesToString := TEncoding.UTF8.GetString(ugly);
248247
end;
249248

250-
// entry_name_array is global, only pass length (K)
251-
// entry_temp_array is global, only pass length (J)
252-
procedure HashAndSave(K: Integer; J: Integer); inline;
249+
250+
procedure HashAndSave;
253251
var
254252
entry_integer: Integer;
255253
entry_Unicode: String;
@@ -259,12 +257,12 @@ procedure HashAndSave(K: Integer; J: Integer); inline;
259257
PlaceIndex: Integer; // location in index array
260258

261259
begin
262-
entry_integer := BytesToTemp(J); // only pass J
260+
entry_integer := BytesToTemp; // only pass J
263261

264-
entry_Unicode := StaticBytesToString(K);
262+
entry_Unicode := StaticBytesToString;
265263
// entry_name_array is global, only pass length
266264

267-
entry_hash := BytesToIndexHash(K);
265+
entry_hash := BytesToIndexHash;
268266
// entry_name_array is global, only pass length
269267

270268
while True do // success is handled by breaking out of while loop
@@ -325,11 +323,8 @@ procedure FileToArrays(inFile: String; UseStdOut: Boolean); inline;
325323
var
326324
PlaceIndex: Integer; // location in index array
327325
i: Integer; // used for temporary purposes
328-
J: Integer; // used for temporary purposes
329-
K: Integer; // used for temporary purposes
330326

331327
LF: Boolean; // True after finding #10
332-
C: Byte;
333328
SC: Boolean; // True after finding semi-colon
334329

335330
iFileHandle: Integer;
@@ -359,8 +354,8 @@ procedure FileToArrays(inFile: String; UseStdOut: Boolean); inline;
359354
// Buffer := System.AllocMem(myBufferSize +1); // why +1 ??
360355
Buffer := System.AllocMem(myBufferSize); // seems OK
361356
i := 0;
362-
J := 0;
363-
K := 0;
357+
entry_temp_array_length := 0;
358+
entry_name_array_length := 0;
364359
LF := False;
365360
SC := False;
366361
move(empty_entry_name_array[0], entry_name_array[0], 256);
@@ -373,31 +368,30 @@ procedure FileToArrays(inFile: String; UseStdOut: Boolean); inline;
373368
begin
374369
while Not LF do // read byte by byte
375370
begin
376-
C := (Buffer + i)^; // get a byte
377-
if (C = 10) then
371+
if ((Buffer + i)^ = 10) then
378372
begin
379373
LF := True; // and done collecting bytes
380374
end
381375
else // accumulate bytes
382376
begin
383-
if (C = 59) then
377+
if ((Buffer + i)^ = 59) then
384378
SC := True;
385379
// skip line feed, carriage return and semi-colon
386-
if Not((C = 10) or (C = 13) or (C = 59)) then
380+
if Not(((Buffer + i)^ = 13) or ((Buffer + i)^ = 59)) then
387381
begin
388382
if SC then
389383
begin
390384
// skip decimal in number
391-
if Not(C = 46) then
385+
if Not((Buffer + i)^ = 46) then
392386
begin
393-
entry_temp_array[J] := C;
394-
Inc(J);
387+
entry_temp_array[entry_temp_array_length] := (Buffer + i)^;
388+
Inc(entry_temp_array_length);
395389
end;
396390
end
397391
else
398392
begin
399-
entry_name_array[K] := C; // don't skip period in name
400-
Inc(K);
393+
entry_name_array[entry_name_array_length] := (Buffer + i)^; // don't skip period in name
394+
Inc(entry_name_array_length);
401395
end;
402396
end;
403397
end;
@@ -410,19 +404,16 @@ procedure FileToArrays(inFile: String; UseStdOut: Boolean); inline;
410404

411405
if LF then // convert to something usable and process it
412406
begin
413-
// entry_name_array and entry_temp_array are static global,
414-
// lengths, i.e. k and j, respectively
415-
// HashAndSave(entry_name_array, k, entry_temp_array, j);
416-
HashAndSave(K, J);
407+
HashAndSave;
417408
end;
418409

419410
end; // of: if LF then
420411

421412
// reset to get next line
422413
LF := False;
423414
SC := False;
424-
J := 0;
425-
K := 0;
415+
entry_temp_array_length := 0;
416+
entry_name_array_length := 0;
426417
move(empty_entry_name_array[0], entry_name_array[0], 256);
427418
// will hold place name
428419
move(empty_entry_temp_array[0], entry_temp_array[0], 16);

entries/bfire/src/bfire.dpr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ begin
3939
// With TFileStream: 6 seconds for 1E7 records,
4040
// 1 min 1 sec for 1E8 records, 10 min 17 sec for 1E9 records
4141

42+
// using global variables for arrays and lengths: 5 seconds for 1E7 records,
43+
// 56 sec for 1E8 records, 9 min 25 sec for 1E9 records
44+
4245
FileToArrays(inputFilename, UseStdOut); // read
4346
SortArrays; // sort
4447
ArrayToFile(outputFilename, UseStdOut); // output

entries/bfire/src/version.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
const
2-
cVersion = '2.0';
2+
cVersion = '2.1';

0 commit comments

Comments
 (0)