//以下三个函数功能一样, 但效率不同

{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;

posted on 2008-01-30 11:18  万一  阅读(4249)  评论(17编辑  收藏  举报