BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Optimization of the source code !
Goto page 1, 2  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
kossy
Guest





PostPosted: Sun Jan 04, 2004 12:21 pm    Post subject: Optimization of the source code ! Reply with quote



How can i optimize my source code? The main procedure, which is to
optimize, is the "Summe20" !



procedure TForm1.Uberprufen (i: Byte); { Überprüft den neuen Wert
}
var k: Byte; { auf Gültigkeit des
Def.bereichs und}
begin { auf Gleichheit mit
vorhergehenden Werten}
ungultigerWert:=FALSE;
gleicherWert:=FALSE;
FOR k:=2 TO (i-1) DO
begin
IF Zahlin [i]<=1 THEN
ungultigerWert:=TRUE;
IF Zahlin [i] = Zahlin [k] THEN
gleicherWert := TRUE;
end;
end;


procedure TForm1.ZahlenZeigen; {Zeigt in der Listbox 9
Zahlen an}
var i: Byte;
begin
FOR i:=1 TO 9 DO
LB.Items.Add ( IntToStr (Zahlin [i]));
end;



procedure TForm1.ZahlenSuchen; {initialisiert Felder}
var i : Byte;
begin
randomize;
For i:=2 TO 9 DO
repeat
begin
Zahlin [i]:= random (Cool+2;
Uberprufen (i);
end
until
(ungultigerWert=FALSE) AND (gleicherWert=FALSE);
end;

procedure TForm1.Umbenennen; {benennt die Felder in
z[1..9] um }
begin
z1:= Zahlin [1];
z2:= Zahlin [2];
z3:= Zahlin [3];
z4:= Zahlin [4];
z5:= Zahlin [5];
z6:= Zahlin [6];
z7:= Zahlin [7];
z8:= Zahlin [8];
z9:= Zahlin [9];
end;

procedure TForm1.Summe20; {Überprüft, ob die Summe der
benötigten }
var a,b,c,d,e,f: Byte; { Felder des Mathespiels die
Summe 20 ergeben }
begin
a:=z1+z3+z7+z9;
b:=z1+z2+z4+z5;
c:=z2+z3+z5+z6;
d:=z4+z5+z7+z8;
e:=z5+z6+z8+z9;
f:=z2+z6+z4+z8;
IF (a=20) THEN
IF (b=20) THEN
IF (c=20) THEN
IF (d=20) THEN
IF (e=20) THEN
IF (f=20) THEN
Summe:=TRUE;
end;

procedure TForm1.MatheSpiel; { Hauptteil }
begin
Zahlin [1]:=1;
Summe:=FALSE;
WHILE Summe=FALSE DO
begin
ZahlenSuchen;
Umbenennen;
Summe20
end;
ZahlenZeigen;
end;



procedure TForm1.BStartenClick(Sender: TObject); {Start }
begin
LB.Clear;
MatheSpiel;
end;

procedure TForm1.BBeendenClick(Sender: TObject);
begin
Close;
end;

end.


Please help!!
kossy
Back to top
Maarten Wiltink
Guest





PostPosted: Sun Jan 04, 2004 1:44 pm    Post subject: Re: Optimization of the source code ! Reply with quote



"kossy" <konstantin (AT) fotin (DOT) de> wrote

Quote:
How can i optimize my source code? The main procedure, which is to
optimize, is the "Summe20" !

procedure TForm1.Umbenennen; {benennt die Felder in
z[1..9] um }
begin
z1:= Zahlin [1];
z2:= Zahlin [2];
z3:= Zahlin [3];
z4:= Zahlin [4];
z5:= Zahlin [5];
z6:= Zahlin [6];
z7:= Zahlin [7];
z8:= Zahlin [8];
z9:= Zahlin [9];
end;

procedure TForm1.Summe20; {Überprüft, ob die Summe der
benötigten }
var a,b,c,d,e,f: Byte; { Felder des Mathespiels die
Summe 20 ergeben }
begin
a:=z1+z3+z7+z9;
b:=z1+z2+z4+z5;
c:=z2+z3+z5+z6;
d:=z4+z5+z7+z8;
e:=z5+z6+z8+z9;
f:=z2+z6+z4+z8;
IF (a=20) THEN
IF (b=20) THEN
IF (c=20) THEN
IF (d=20) THEN
IF (e=20) THEN
IF (f=20) THEN
Summe:=TRUE;
end;

To start with, that's Summe:=(z1+z3+z7+z9=20) and (z1+z2+z4+z5=20) and ...;
but that does little towards actual optimisation. What you want is a
better algorithm. That comes with a better design.

It looks like some situation with a concept of "win-lines", which in this
case must _all_ fulfill some condition. You'd probably benefit most from
first modeling your playing field in two dimensions, and then modeling the
"win-lines" in some manner that lets you pass a bunch of numbers to a
single function that evaluates the sum along that line.

All of which is reminiscent, of course, of tic-tac-toe. I happened to
write (and document to an extent) a version of that which is for display
at my website. http://huizen.dds.nl/~mfw/programmingstyle.html The code
you want to see is in the CheckWinner function.

Groetjes,
Maarten Wiltink



Back to top
Maarten Wiltink
Guest





PostPosted: Sun Jan 04, 2004 1:47 pm    Post subject: Re: Optimization of the source code ! Reply with quote



"Maarten Wiltink" <maarten (AT) kittensandcats (DOT) net> wrote

Quote:
"kossy" <konstantin (AT) fotin (DOT) de> wrote in message
news:ae2d009e.0401040421.14fb97c3 (AT) posting (DOT) google.com...
How can i optimize my source code? The main procedure, which is to
optimize, is the "Summe20" !

procedure TForm1.Umbenennen; { benennt die Felder in z[1..9] um }
begin
z1:= Zahlin [1];
z2:= Zahlin [2];
z3:= Zahlin [3];
z4:= Zahlin [4];
z5:= Zahlin [5];
z6:= Zahlin [6];
z7:= Zahlin [7];
z8:= Zahlin [8];
z9:= Zahlin [9];
end;

Ah yes, I forgot to say. This is of course just plain silly. If you
feel called upon to do this, you don't want an array in the first
place. (Or at any rate, not the one you currently have. _You_ probably
do want a two-dimensional array.)

Groetjes,
Maarten Wiltink



Back to top
AlanGLLoyd
Guest





PostPosted: Sun Jan 04, 2004 1:53 pm    Post subject: Re: Optimization of the source code ! Reply with quote

In article <ae2d009e.0401040421.14fb97c3 (AT) posting (DOT) google.com>,
[email]konstantin (AT) fotin (DOT) de[/email] (kossy) writes:

Quote:
How can i optimize my source code? The main procedure, which is to
optimize, is the "Summe20" !


Firstly ...

Summe := (a=20) and (b=20) and (c=20) and (d=20) and (e=20) and (f=20);

... or ...

var
ResultSet : set of byte;
begin
ResultSet := [];
Include(z1+z3+z7+z9, ResultSet);
Include(z1+z2+z4+z5, ResultSet);
Include(z2+z3+z5+z6, ResultSet);
Include(z4+z5+z7+z8, ResultSet);
Include(z5+z6+z8+z9, ResultSet);
Include(z2+z6+z4+z8, ResultSet);

Summe := (ResultSet = [20]);

And then by passing parameters of values needed in each method, rather than by
referencing outside the method. Reduce the number of global variables.

Alan Lloyd
[email]alanglloyd (AT) aol (DOT) com[/email]

Back to top
kossy
Guest





PostPosted: Tue Jan 06, 2004 1:36 am    Post subject: Re: Optimization of the source code ! Reply with quote

Thanks, but i do not want to optimize my system memory but processor
performance.

I think i have to search for a solution with the help of probability
calculus.


i think it was my fault not to explain the play, which deals with
geometric sum of 20. the play field consists of three columns and
three lines. the sum of 20 results from corners of each of 6 quadrat,
like following:

z1 z2 z3

z4 z5 z6

z7 z8 z9

z1,z3,z7,z8=20
z1,z2,z4,z5=20 etc.

(do not forget the z2,z6,z8,z4 quadrat)


here is my improved source code but unfortunately not the "highest
truth" of it ;)



function TForm1.Ungultig (i: Byte):BOOLEAN; { Überprüft den neuen
Wert }
var k: Byte; { auf Gültigkeit des
Def.bereichs und}
begin { auf Gleichheit mit
vorhergehenden Werten}
Ungultig:=FALSE;
FOR k:=2 TO (i-1) DO
IF (Zahlin [i]<=1) OR (Zahlin [i] = Zahlin [k])
THEN Ungultig:=TRUE;
end;


procedure TForm1.ZahlenZeigen; {Zeigt in der Listbox 9
Zahlen an}
var i: Byte;
begin
FOR i:=1 TO 9 DO
LB.Items.Add ( IntToStr (Zahlin [i]));
end;



procedure TForm1.ZahlenSuchen; {initialisiert Felder}
var i : Byte;
begin
randomize;
For i:=2 TO 9 DO
repeat
begin
Zahlin [i]:= random (Cool+2;
Ungultig (i);
end
until
Ungultig (i)=FALSE;
end;

procedure TForm1.Umbenennen (var z1,z2,z3,z4,z5,z6,z7,z8,z9: Byte);
{benennt die Felder in z[1..9] um }
begin
z1:= Zahlin [1];
z2:= Zahlin [2];
z3:= Zahlin [3];
z4:= Zahlin [4];
z5:= Zahlin [5];
z6:= Zahlin [6];
z7:= Zahlin [7];
z8:= Zahlin [8];
z9:= Zahlin [9];
end;

function TForm1.Summe20 (z1,z2,z3,z4,z5,z6,z7,z8,z9: Byte):BOOLEAN;
{Überprüft, ob die Summe der benötigten }
var a,b,c,d,e,f: Byte; { Felder des Mathespiels die
Summe 20 ergeben }
begin
Result:=FALSE;
a:=z1+z3+z7+z9;
b:=z1+z2+z4+z5;
c:=z2+z3+z5+z6;
d:=z4+z5+z7+z8;
e:=z5+z6+z8+z9;
f:=z2+z6+z4+z8;
IF (a=20) and (b=20) and (c=20) and (d=20) and (e=20) and (f=20)
THEN Result:=TRUE;
end;

procedure TForm1.MatheSpiel; { Hauptteil }
var z1,z2,z3,z4,z5,z6,z7,z8,z9 :Byte;
begin
Zahlin [1]:=1;
WHILE Summe20 (z1,z2,z3,z4,z5,z6,z7,z8,z9)=FALSE DO
begin
ZahlenSuchen;
Umbenennen (z1,z2,z3,z4,z5,z6,z7,z8,z9);
Summe20 (z1,z2,z3,z4,z5,z6,z7,z8,z9);
end;
ZahlenZeigen;
end;



procedure TForm1.BStartenClick(Sender: TObject); {Start }
begin
LB.Clear;
MatheSpiel;
end;

procedure TForm1.BBeendenClick(Sender: TObject);
begin
Close;
end;

end.
Back to top
Nicolai Hansen
Guest





PostPosted: Tue Jan 06, 2004 7:51 am    Post subject: Re: Optimization of the source code ! Reply with quote

Quote:
function TForm1.Summe20 (z1,z2,z3,z4,z5,z6,z7,z8,z9: Byte):BOOLEAN;
{Überprüft, ob die Summe der benötigten }
var a,b,c,d,e,f: Byte; { Felder des Mathespiels die
Summe 20 ergeben }
begin
Result:=FALSE;
a:=z1+z3+z7+z9;
b:=z1+z2+z4+z5;
c:=z2+z3+z5+z6;
d:=z4+z5+z7+z8;
e:=z5+z6+z8+z9;
f:=z2+z6+z4+z8;
IF (a=20) and (b=20) and (c=20) and (d=20) and (e=20) and (f=20)
THEN Result:=TRUE;
end;

function TForm1.Summe20(z1, z2, z3, z4, z5, z6, z7, z8, z9: Byte):
Boolean;
var
res: byte;
begin
asm
push ax
mov res, $00
mov al, $14
sub al, z1
sub al, z3
sub al, z7
sub al, z9
jnz @not20
mov al, $14
sub al, z1
sub al, z2
sub al, z4
sub al, z5
jnz @not20
mov al, $14
sub al, z2
sub al, z3
sub al, z5
sub al, z6
jnz @not20
mov al, $14
sub al, z4
sub al, z5
sub al, z7
sub al, z8
jnz @not20
mob al, $14
sub al, z5
sub al, z6
sub al, z8
sub al, z9
jnz @not20
mov al, $14
sub al, z2
sub al, z6
sub al, z8
sub al, z9
jnz @not20
mov res, $01
@not20:
pop ax
end;

result:=(res=$01);
end;

But if you really need optimisation, I'm sure there is an
algorithmic/mathematically way of doing so, too.
Maybe this pascal code would generate much of the same code:

begin
result:=false;
a:=z1+z3+z7+z9;
if not (a=20) then exit;
a:=z1+z2+z4+z5;
if not (a=20) then exit;
.... (etc)
result:=true;
end;

Generally, use only one variable for storing intermediate results.
Exit right after the summation if it is not 20. This will save the
executable for a lot of uneeded instructions. Generally I would also
check on (a=0) instead of (a=20), as the resulting code changes from a
"cmp, jne" to a simply "jnz" of optimised correctly. Not sure Delphi
is able to do this optimisation, though.
And I didn't test the assembly code so it might not work at all ;)

Back to top
Tom de Neef
Guest





PostPosted: Tue Jan 06, 2004 11:47 am    Post subject: Re: Optimization of the source code ! Reply with quote


"kossy" <konstantin (AT) fotin (DOT) de> wrote

Quote:
Thanks, but i do not want to optimize my system memory but processor
performance.

I think i have to search for a solution with the help of probability
calculus.


i think it was my fault not to explain the play, which deals with
geometric sum of 20. the play field consists of three columns and
three lines. the sum of 20 results from corners of each of 6 quadrat,
like following:

z1 z2 z3

z4 z5 z6

z7 z8 z9

z1,z3,z7,z9=20
z1,z2,z4,z5=20 etc.

(do not forget the z2,z6,z8,z4 quadrat)


I assume that you are after solutions with positive integers.
If you go about it the rough way, testing every field for values between 0
and 20, then you loop about 5x10^11 times. Just too much for your desktop.
This may be an example where assembler helps in reducing the time needed by
a factor of three but where another approach may reduce the time by a factor
of a million. Let's try.
First of all, once z1 has a value, z3 need loop only between 0 and 20-z1.
And then z7 = 0..20-z1-z3; z9=0..20-z1-z3-z7.
Similar rules apply to the other variables. This reduces the effort by 2-3
orders of magnitude.

You can also make it more symmetric, showing the solution
(5,5,5,5,5,5,5,5,5) but I don't see this helping the algorithm.

Adding and subtracting the equations does help though.
Denote the equations by TL for the topleft 4 squares (z1+z2+z4+z5), TR, BL,
BR, C (cross = z1+z3+z7+z9) and P (plus = z2+z4+z6+zCool. TL-X eliminates z1.
TL-TR eliminates z2 and z5, etc.
Eliminate all but z5 via TL+TR+BL+BR-2P-C = 4z5 = 20, or z5=5, universally.
In this way I also find that 3BR-TR-TL-BL+C+2P = 4(z6+z8+z9) = 60. Or
z6+z8+z9=15, universally.

z9=15-z6-z8;
z2+z4=20-(z6+zCool = 20-(15-z9) = z9+5.
z1=15-(z2+z4) = 15-(z9+5) = 10-z9.
Hence, z9 can not be larger than 10; z6+z8>=5.
By virtue of symmetry,: z3+z7=10, like z1+z9=10.

Take z6, z8 and z2 as independents:
z5 = 5
z9 = 15-z6-z8
z1 = 10-z9
z4 = 15-z1-z2
z7 = 15-z8-z4
z3 = 10-z7

Thus, your algorithm would be
z5 := 5;
for z6:=0 to 15 do
for z8:=0 to 15-z6 do
for z2:=0 to 15 do
begin
z9 := 15-z6-z8; // z9 in 0..15
z1 := 10-z9; // z1 in -5..10
if z1<0 then continue; // z1 in 0..10
z4 := 15-z1-z2; // z4 in -10..15
if z4<0 then continue; // z4 in 0..15
z7 := 15-z8-z4;
if z7<0 then continue;
z3 := 10-z7;
if z3>=0 then writeln(z1,z2,z3,z4,z5,z6,z7,z8,z9)
end;

I'm sure you can also work the first two if statements into the start and
end values of the loops. But this algorithm will cycle only 3000 times, so
why bother...

Tom




Back to top
Tom de Neef
Guest





PostPosted: Tue Jan 06, 2004 1:02 pm    Post subject: Re: Optimization of the source code ! Reply with quote

"Tom de Neef" <tdeneef (AT) qolor (DOT) nl> wrote

Quote:

"kossy" <konstantin (AT) fotin (DOT) de> wrote in message
news:ae2d009e.0401051736.56d97cbe (AT) posting (DOT) google.com...
Thanks, but i do not want to optimize my system memory but processor
performance.

I think i have to search for a solution with the help of probability
calculus.


There are, I think, 891 solutions with all z>=0.
If the problem is bigger (eg on a 4x4 square), you could also use the
intrinsic symmetry. Rotating a solution over 90 degrees gives another one;
flipping around a diagonal or a vertical or horizontal axis also results in
another solution and you can also combining these. Some of them transform
into themselves. I guess that there are about 60 independent solutions.
Tom



Back to top
Nicolai Hansen
Guest





PostPosted: Tue Jan 06, 2004 3:46 pm    Post subject: Re: Optimization of the source code ! Reply with quote

Quote:
I assume that you are after solutions with positive integers.

And don't think the solution is limited to positive integers.

Quote:
Adding and subtracting the equations does help though.
Eliminate all but z5 via TL+TR+BL+BR-2P-C = 4z5 = 20, or z5=5, universally.
In this way I also find that 3BR-TR-TL-BL+C+2P = 4(z6+z8+z9) = 60. Or
z6+z8+z9=15, universally.

Yep, and considering symmetry,

z1+z2=z7+z8, z2+z3=z8+z9, z1+z4=z3+z6, and z4+z7=z6+z9.

Considering this, we can give z1 the value A. z2 will then have a
value relative to z1, say A+B. z7 will have another value relative to
z1, say A+C.
z8 will then have the value A+B+C.
z3 will also have a value relative to A, say A+D. z9 will then have
the value A+C+D.
finally, z4 will have the value A+E, and z6 the value A+D+E.

So we actually have 5 variables in the system.

A A+B A+D
A+E 5 A+D+E
A+C A+B+C A+C+D

we can then write our original equations:

1: A+(A+D)+(A+C)+(A+C+D)=20 = 4*A+2*C+2*D
2: A+(A+B)+(A+E)=15 = 3*A+B+E
3: (A+B)+(A+D)+(A+D+E)=15 = 3*A+B+2*D+E
4: (A+E)+(A+C)+(A+B+C)=15 = 3*A+B+2*C+E
5: (A+D+E)+(A+B+C)+(A+C+D)=15 = 3*A+B+2*C+2*D+E
6: (A+B)+(A+E)+(A+D+E)+(A+B+C)=20 = 4*A+2*B+C+D+2*E

inserting 2 into 3 gives 2*D=0 => D=0.
inserting 2 into 4 gives 2*C=0 => C=0.

and therefore

1: 20=4*A => A=5

2: B=-E

So the matrix needs to look like:

5 5+X 5
5-X 5 5-X
5 5+X 5

all other solutions will not be valid.

So what we are looking for is one, and only one, number Smile And any
number will do.

/Nic

Back to top
Tom de Neef
Guest





PostPosted: Tue Jan 06, 2004 4:57 pm    Post subject: Re: Optimization of the source code ! Reply with quote


"Nicolai Hansen" <nic (AT) aub (DOT) dk> wrote

Quote:
I assume that you are after solutions with positive integers.

And don't think the solution is limited to positive integers.

Adding and subtracting the equations does help though.
Eliminate all but z5 via TL+TR+BL+BR-2P-C = 4z5 = 20, or z5=5,
universally.
In this way I also find that 3BR-TR-TL-BL+C+2P = 4(z6+z8+z9) = 60. Or
z6+z8+z9=15, universally.

Yep, and considering symmetry,

z1+z2=z7+z8, z2+z3=z8+z9, z1+z4=z3+z6, and z4+z7=z6+z9.

Why would symmetry imply this?

What about a solution such as
1 8 7
6 5 0
3 6 9

Tom



Back to top
Tom de Neef
Guest





PostPosted: Tue Jan 06, 2004 7:16 pm    Post subject: Re: Optimization of the source code ! Reply with quote


"Nicolai Hansen" <nic (AT) aub (DOT) dk> wrote

Quote:
I assume that you are after solutions with positive integers.

And don't think the solution is limited to positive integers.

Adding and subtracting the equations does help though.
Eliminate all but z5 via TL+TR+BL+BR-2P-C = 4z5 = 20, or z5=5,
universally.
In this way I also find that 3BR-TR-TL-BL+C+2P = 4(z6+z8+z9) = 60. Or
z6+z8+z9=15, universally.

Yep, and considering symmetry,

z1+z2=z7+z8, z2+z3=z8+z9, z1+z4=z3+z6, and z4+z7=z6+z9.


I missed this at first but you're right: the three fields around a corner
add up to 15 (as I saide above: z6+z8+z9=15, and hence z2+z3+z6).
Subtracting two such equations yields the equalities you quote.

Quote:
Considering this, we can give z1 the value A. z2 will then have a
value relative to z1, say A+B. z7 will have another value relative to
z1, say A+C.
z8 will then have the value A+B+C.

I think you messed up the signs here:
z8=z1+z2-z7=A+(A+B)-(A+C) = A+B-C
and hence the rest of the argument goes astray.

The matrix satisfying all 6 equations has the form:
5+z1 5+z2 5+z3
5-z1-z2 5 5-z2-z3
5-z3 5+z1+z2+z3 5-z1

And you can choose z1, z2 and z3 independently.
Tom




Back to top
Nicolai Hansen
Guest





PostPosted: Wed Jan 07, 2004 7:21 am    Post subject: Re: Optimization of the source code ! Reply with quote

This will be my last attempt to post this; the router is flying up and
down and 50% of the time I don't have internet connection :/

Quote:
I think you messed up the signs here:
z8=z1+z2-z7=A+(A+B)-(A+C) = A+B-C
and hence the rest of the argument goes astray.

Yep, you are right. The following is right too:

Quote:
The matrix satisfying all 6 equations has the form:
5+z1 5+z2 5+z3
5-z1-z2 5 5-z2-z3
5-z3 5+z1+z2+z3 5-z1

And you can choose z1, z2 and z3 independently.

This won't be too much of an optimisation, though, unless the method
creating the data is made to find values for z1, z2, z3 instead.

Back to top
Martin Harvey (Demon Acco
Guest





PostPosted: Thu Jan 08, 2004 7:02 pm    Post subject: Re: Optimization of the source code ! Reply with quote

On 5 Jan 2004 17:36:22 -0800, [email]konstantin (AT) fotin (DOT) de[/email] (kossy) wrote:

Quote:
Thanks, but i do not want to optimize my system memory but processor
performance.

In that case, don't use byte wide values - make them 32 bit quantities
instead. Even the assembler optimised version someone posted later on
has a horrendous cost (about 300 cycles when it should be 50) - almost
all of these are caused by partial register stalls.

You should see a 2-fold performance gain straight away.

After you've done that, I have some *very* fancy ideas for
aprallelising the computation.

What sort (make and model) of CPU do you want to optimise for?

MH.

Back to top
Richard Atkinson
Guest





PostPosted: Tue Jan 13, 2004 2:44 am    Post subject: Re: Optimization of the source code ! Reply with quote

In news:d96764ff.0401062321.46c58dc3 (AT) posting (DOT) google.com,
Nicolai Hansen <nic (AT) aub (DOT) dk> had the generosity to contribute:

Quote:
The matrix satisfying all 6 equations has the form:
5+z1 5+z2 5+z3
5-z1-z2 5 5-z2-z3
5-z3 5+z1+z2+z3 5-z1

Hence there are three degrees of freedom, so of the 9 values, you only need
to test 6. With a bit of rearrangement, the matrix can be rewritten:

A B C
15-(A+B) 5 15-(B+C)
10-C A+B+C-10 10-A

Do the tests in order of simplest to most complicated (see comments below),
and save temporary sum:

Result := False;
if z5 <> 5 then Exit;
if z7 <> 10 - z3 then Exit;
if z9 <> 10-z1 then Exit;
Temp := z1 + Z2;
if z4 <> 15 - Temp then Exit;
if z6 <> 15 - z2 - z3 then Exit;
if z8 <> Temp + z3 - 10 then Exit;
Result := True;

I don't promise this is optimal, but it's better.

Note that the original code makes some assumptions about the range of the
values, as the temp variables are byte. In my testing I made the temps into
integer.

I tested both these methods calling them 10 million times each with random
values for all cells between -10 and 10 with range checking etc switched
off, and optimization switched on.

On my 745MHz Pentium the results were:

Original average run time 34ns
My code average run time 8ns

Measurements using ProDelphi.

Without delving too deeply into architecture dependent issues, one further
optimization would be to look at the relative likelihoods of each of the 6
tests being true, compared with the time taken to perform the test. This
would indicate the best order to perform the tests, as one failure removes
the need for further tests. To do this you need to know the range of
possible values in each cell.

FWIW Richard



Back to top
Nicolai Hansen
Guest





PostPosted: Tue Jan 13, 2004 7:51 am    Post subject: Re: Optimization of the source code ! Reply with quote

Quote:
I tested both these methods calling them 10 million times each with random
values for all cells between -10 and 10 with range checking etc switched
off, and optimization switched on.

On my 745MHz Pentium the results were:

Original average run time 34ns
My code average run time 8ns

Hmm.. this is us (micro seconds), not ns (nano seconds), right? Else I
would call it a pretty mean procedure Wink That is an average of 93
clock cycles per run. This is a lot, but I think most of it is the
random numbers you need to generate. Just made a small program to try
and compile the code and look at the resulting assembler - about 35
instructions for a full run (a lot of cmp dword ptr instructions
though).

Quote:
Temp := z1 + Z2;
if z4 <> 15 - Temp then Exit;
if z6 <> 15 - z2 - z3 then Exit;
if z8 <> Temp + z3 - 10 then Exit;

can be optimised by putting the z6 line on top. No need to calculate
the "temp" variable if that statement is false anyway.

Actually, another optimisation _might_ be to make the function stdcall
and change the parameters to the right order of usage.

function Summe20(z8,z4,z6,z2,z9,z1,z7,z3,z5: integer): boolean;
stdcall;

would put the vars on the stack in the order we need 'em ;)

begin
result:=false;
if z5<>5 then exit; // pop eax; cmp eax, $00000005; jnz ?
if z7+z3<>10 then exit; // pop ebx; pop eax; add eax, ebx; cmp eax,
$0000000a; jnz ?
if z9+z1<>10 then exit; // pop ecx; pop eax; add eax, ecx; cmp eax,
$0000000a; jnz ?
if z6+z2+z3<>15 then exit; // pop edx; pop eax; add eax, ebx; add
eax, edx; cmp eax, $0000000f; jnz ?
temp:=z1+z2; // add ecx, edx;
if z4+temp<>15 then exit; // pop eax; add eax, ecx; cmp eax,
$0000000f; jnz ?
if z8+temp+z3+10<>0 then exit; // pop eax; add eax, ecx; add eax,
ebx; add eax, $0000000a; jnz ?
result:=true;
end;

something like that. the compiler might be able to generate this code
itself :)

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.