Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does function overloading have runtime overhead in Delphi?

Is there any additional runtime overhead in calling overloaded functions?

(I ask this specifically for Delphi, in case the answer isn't the same for all compiled languages)

I think not as that should be resolved during compile time, but you can never be sure can you?

like image 332
Darx Avatar asked Sep 28 '10 19:09

Darx


People also ask

What is overload in Delphi?

Simply put, overloading is declaring more than one routine with the same name. Overloading allows us to have multiple routines that share the same name, but with a different number of parameters and types.

How does function overloading work?

Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading.

Is function overloading compile time ++?

Method/function overloading is an implementation of compile-time polymorphism where the same name can be assigned to more than one method or function, having different arguments or signatures and different return types.


1 Answers

Of course you can be sure, because it is documented. Is the compiler which resolves it at compile time, so there's no additional overhead on calling overloaded functions in Delphi.

[Edit]

I did a small test for you:

var
  j: Integer;
  st: string;

procedure DoNothing(i: Integer); overload;
begin
  j := i;
end;

procedure DoNothing(s: string); overload;
begin
  st := s;
end;

procedure DoNothingI(i: integer);
begin
  j := i;
end;

procedure TForm2.Button1Click(Sender: TObject);
const
  MaxIterations = 10000000;
var
  StartTick, EndTick: Cardinal;
  I: Integer;
begin
  StartTick := GetTickCount;
  for I := 0 to MaxIterations - 1 do
    DoNothing(I);
  EndTick := GetTickCount;
  Label1.Caption := Format('Overlaod ellapsed ticks: %d [j:%d]', [EndTick - StartTick, j]);
  StartTick := GetTickCount;
  for I := 0 to MaxIterations - 1 do
    DoNothingI(I);
  EndTick := GetTickCount;
  Label1.Caption := Format('%s'#13'Normal ellapsed ticks: %d [j:%d]', [Label1.Caption, EndTick - StartTick, j]);
end;

Result: Almost all the time 31 Ticks (milliseconds) for both on my dev machine, sometimes overload takes only 16 ticks.

alt text

like image 133
jachguate Avatar answered Sep 27 '22 22:09

jachguate