//以下三個函數功能一樣, 但效率不同
{Fun1 需要讀取常數 0, 最慢}
function Fun1: Integer;
asm
mov eax, 0
end;
{Fun2 與 Fun3 只是操作 CPU 的寄存器, 比 Fun1 快}
function Fun2: Integer;
asm
sub eax, eax
end;
{Fun3 最快}
function Fun3: Integer;
asm
xor eax, eax
end;
//速度測試
procedure TForm1.Button1Click(Sender: TObject);
var
t: Cardinal;
i: Integer;
begin
t := GetTickCount;
for i := 0 to 100000000 do Fun1;
t := GetTickCount - t;
ShowMessage(IntToStr(t)); {均: 600 多}
t := GetTickCount;
for i := 0 to 100000000 do Fun2;
t := GetTickCount - t;
ShowMessage(IntToStr(t)); {均: 500 多}
t := GetTickCount;
for i := 0 to 100000000 do Fun3;
t := GetTickCount - t;
ShowMessage(IntToStr(t)); {均: 400 多}
end;