调用重载函数是否有额外的运行时开销?
(我专门针对Delphi提出这个问题,以防所有编译语言的答案都不一样)
我不认为这应该在编译时解决,但你永远不能确定,不是吗?
发布于 2010-09-29 03:16:20
您当然可以确定,因为它是documented。是在编译时解析它的编译器,因此在Delphi中调用重载函数不会产生额外的开销。
编辑
我给你做了个小测试:
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;结果:在我的dev机器上,几乎所有的时间都是31刻度(毫秒),有时重载只需要16刻度。

https://stackoverflow.com/questions/3816178
复制相似问题