View Full Version : To good optimization?
pstudio
15-10-2007, 07:35 PM
So I have a pretty simple function
function MyPoint(X,Y: Integer): TMyPoint;
begin
Result.X := X;
Result.Y := Y;
end;
I can't see anything wrong with this, but when I test my project all my points ends up with coordinate (0,0). I try and add some breakpoints and can see that delphi can't acces variable X and Y due to optimization :?
Can someone tell me what's going on?
EDIT. Nevermind - it turns out the problem is not there.
CheGueVerra
11-01-2008, 11:16 PM
Uncheck the Optimization in the Project/Options Compiler Tab
and select Stack frames you will see the values
CheGueVerra
chronozphere
12-01-2008, 11:10 AM
It's alway's a good idea to use Const.... like this:
function MyPoint(Const X,Y: Integer): TMyPoint;
begin
Result.X := X;
Result.Y := Y;
end;
This will speed things up, especially mathy routines with lots of params. :)
Just to let you know. ;)
Setharian
13-01-2008, 07:15 PM
Const with integers doesn't change the code generated. ;)
arthurprs
13-01-2008, 09:24 PM
Const with integers doesn't change the code generated. ;)
But they clarify the code :)
chronozphere
13-01-2008, 10:08 PM
Const with integers doesn't change the code generated. Wink
Hmmm... alway's thought it would speed things up... does it do so when using other types :? ?
arthurprs
13-01-2008, 11:25 PM
Const with integers doesn't change the code generated. Wink
Hmmm... alway's thought it would speed things up... does it do so when using other types :? ?
When using const in parameters the function does not copy the parameter data to a local variable on the function, it actually works as a pointer to the original parameter data (or something like that)
cronodragon
14-01-2008, 05:27 AM
Const is for string, record and array parameters. :)
Setharian
14-01-2008, 06:17 AM
You forgot interfaces and also sets larger than native integer size (4 bytes on 32-bit, 8 bytes on 64-bit). :P
User137
14-01-2008, 12:11 PM
Ok.. assume this gets optimized to pointer even without const:
function MyPoint(X,Y: Integer): TMyPoint;
begin
Result.X := X;
Result.Y := Y;
end;
But does compiler then recognize to not optimize this?
function MyPoint(X,Y: Integer): TMyPoint;
begin
X:=X+10; // If it was pointer it would modify origin...
Result.X := X;
Result.Y := Y;
end;
Setharian
14-01-2008, 01:49 PM
It's still passed by value. You'd have to explicitly pass the integers as pointers by using "var". Then the "X := X + 10;" line would modify the source variable. Otherwise just the local copy of the variable is modified.
Brainer
14-01-2008, 04:38 PM
It's still passed by value. You'd have to explicitly pass the integers as pointers by using "var". Then the "X := X + 10;" line would modify the source variable. Otherwise just the local copy of the variable is modified.
He's right. Compare these:
function MyPoint(X, Y: Integer): TMyPoint;
begin
X := X + 10;
Result.X := X;
Result.Y := Y;
end;
var
X, Y: Integer;
begin
X := 10;
Y := 20;
MyPoint(X, Y);
writeln(X);
readln;
end.
function MyPoint(var X, Y: Integer): TMyPoint;
begin
X := X + 10;
Result.X := X;
Result.Y := Y;
end;
var
X, Y: Integer;
begin
X := 10;
Y := 20;
MyPoint(X, Y);
writeln(X);
readln;
end.
In the first case, X after passing to MyPoint still equals 10. But in the latter, X is passed as a reference, thus it equals 20.
You can also use Assembler to boost up your program:
function MyPoint(X, Y: Integer): TMyPoint;
// EAX contains address of X
// EDX contains address of Y
// ECX contains the result
asm
mov [ECX], EAX
mov [ECX+4], EDX
end;
But I doubt you'll gain any speed using this.
arthurprs
14-01-2008, 06:58 PM
Const is for string, record and array parameters. :)
For anything with more than 4bytes in size
cronodragon
14-01-2008, 08:41 PM
Const is for string, record and array parameters. :)
For anything with more than 4bytes in size
Hmm, a method type (procedure of object) has 8 bytes and I haven't seen any tip about passing it with Const. :P What the Delphi Help says is that const should be used with structure types.
chronozphere
14-01-2008, 10:48 PM
Okay... thanx for the info... realy usefull ;)
arthurprs
15-01-2008, 04:00 AM
Const is for string, record and array parameters. :)
For anything with more than 4bytes in size
Hmm, a method type (procedure of object) has 8 bytes and I haven't seen any tip about passing it with Const. :P What the Delphi Help says is that const should be used with structure types.
Maybe its an "exeption" (not sure about this word)
JSoftware
15-01-2008, 05:32 AM
What const does is simply declare it as a constant in the function. The function can thus not alter it on syntax level, and the compiler then knows that all variables can be passed by reference. This is only a good thing if the parameter is very big, like a string or a big record
With simple types you don't need it
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.