Skip to content

Commit

Permalink
chg: shuffle code for faster codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
martok committed Jan 15, 2014
1 parent bc624c8 commit 5532927
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
22 changes: 17 additions & 5 deletions uAnt.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

interface

{$OPTIMIZATION ON}

uses
SysUtils, uAntWorld;

Expand All @@ -10,8 +12,9 @@ EAntException = class(Exception);

TAnt = class
X, Y: integer;
Face: byte;
Face: Integer;
Rule: array of boolean;
RuleLength: integer;
constructor Create(const aHHP: Cardinal);

procedure SetHHP(const aHHP: Cardinal);
Expand Down Expand Up @@ -60,6 +63,7 @@ procedure TAnt.SetHHP(const aHHP: Cardinal);
SetLength(Rule, Length(rtmp));
for i:= 0 to high(Rule) do
Rule[i]:= rtmp[high(Rule) - i];
RuleLength:= Length(Rule);
end;

function TAnt.GetHHP: Cardinal;
Expand Down Expand Up @@ -98,20 +102,28 @@ function TAnt.Run(const aWorld: TAntWorld): boolean;
(3,0,1,2)
);
var
curr: byte;
curr, next, f: Integer;
begin
inc(aWorld.Generation);

curr:= aWorld.Field[X,Y];
aWorld.Field[X,Y]:= (curr + 1) mod length(Rule);

Face:= newFace[Rule[curr], Face];
case Face of
// next:= (curr+1) mod RuleLength, but faster
next:= curr+1;
if next >= RuleLength then
dec(next, RuleLength);

aWorld.Field[X,Y]:= next;

f:= newFace[Rule[curr], Face];
case f of
FACE_RIGHT: inc(X);
FACE_UP: inc(Y);
FACE_LEFT: dec(X);
FACE_DOWN: dec(Y);
end;
Face:= f;

Result:= aWorld.WrapCoords(X,Y);
end;

Expand Down
10 changes: 4 additions & 6 deletions uAntWorld.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

interface

{$OPTIMIZATION ON}

uses
Windows, SysUtils, Graphics;

Expand Down Expand Up @@ -123,16 +125,12 @@ procedure TAntWorld.SetAntPalette(const aPalette: TAntPalette; const aCount: int

function TAntWorld.GetField(X, Y: integer): byte;
begin
if WrapCoords(X, Y) then
Result:= fpixels[Y]^[X]
else
Result:= 0;
Result:= fpixels[Y]^[X]
end;

procedure TAntWorld.SetField(X, Y: integer; const Value: byte);
begin
if WrapCoords(X, Y) then
fpixels[Y]^[X]:= Value;
fpixels[Y]^[X]:= Value;
end;

function TAntWorld.WrapCoords(var X, Y: integer): boolean;
Expand Down
2 changes: 1 addition & 1 deletion uViewForm.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ object fmTurmites: TfmTurmites
MaxValue = 100000000
MinValue = 100
TabOrder = 0
Value = 50000
Value = 500000
end
object cbPause: TCheckBox
Left = 7
Expand Down
8 changes: 4 additions & 4 deletions uViewForm.pas
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ procedure TfmTurmites.tmrSimTimer(Sender: TObject);
break;
end;
end;
t:= GetTickCount;
genpers:= 0.5 * (genpers + (fWorld.Generation-pg)/((t-lastgentime+1)/1000));
lastgentime:= t;
lbGenPerS.Caption:= Format('%8.3f M/s', [genpers/1E6]);
Label1.Caption:= IntToStr(fWorld.Generation);
Refresh;
if m < 100 then
Sleep(2)
else
if m < 10000 then
Sleep(0);
t:= GetTickCount;
genpers:= 0.5 * (genpers + (fWorld.Generation-pg)/((t-lastgentime+1)/1000));
lastgentime:= t;
lbGenPerS.Caption:= Format('%8.2f M/s', [genpers/1E6]);
end;

procedure TfmTurmites.BeginComp(hhp: Cardinal);
Expand Down

0 comments on commit 5532927

Please sign in to comment.