Let's see this example:
Before:
begin
if FDataLink.Field <> nil then
Caption := FDataLink.Field.DisplayText
else
Caption := '';
end;
After:
var
DataLinkField: TField;
begin
DataLinkField := FDataLink.Field;
if DataLinkField <> nil then
Caption := DataLinkField.DisplayText
else
Caption := '';
end;
It seems that yes, although very little (saves only two instructions). This is the kind of optimization to be done on only very sensitive areas.
Since the benefit was mainly due to the compiler saving the local variable in a register, a doubt that i had in mind was what would happen in a method with many parameters? The addition of the variable would still be beneficial?
So i tested the addition of a variable in a method with the following header
procedure DoIt(Sender, Sender2, Sender3: TObject);
As we can see, the version with the local variable is still smaller.
All in all, some like to say that less is more, but sometimes, as in this case, more is less!
No comments:
Post a Comment