19.03.2018, 10:23
|
|
I Like it!
|
|
Регистрация: 12.12.2009
Адрес: Россия, г. Новосибирск
Сообщения: 660
Версия Delphi: D6/D7
Репутация: 26643
|
|
Не быстрее Round, но быстрее Trunc.
Код:
function Trunc_Dbl(Value: Double): Integer;
var
d_w: array[0..3] of word absolute Value;
begin
Result := ((d_w[3] shl 11) or (d_w[2] shr 5) or $8000) shr (14 - (d_w[3] shr 4 and $03FF));
end;
function Trunc_Asm(Value: Double): Integer;
const
Half: double = 0.5;
begin
asm
fld Value
fsub Half
fistp Result
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
tc1, tc2, tc3, tc4: Cardinal;
_value: Double;
_result: Integer;
begin
_value := 100.56;
tc1 := GetTickCount;
for I := 0 to $FFFFFF do
begin
_result := Trunc(_value);
if _result = 99 then
;
end;
tc1 := GetTickCount - tc1;
//-------------
tc2 := GetTickCount;
for I := 0 to $FFFFFF do
begin
_result := Trunc_Asm(_value);
if _result = 99 then
;
end;
tc2 := GetTickCount - tc2;
//-------------
tc3 := GetTickCount;
for I := 0 to $FFFFFF do
begin
_result := Trunc_Dbl(_value);
if _result = 99 then
;
end;
tc3 := GetTickCount - tc3;
//-------------
tc4 := GetTickCount;
for I := 0 to $FFFFFF do
begin
_result := Round(_value);
if _result = 99 then
;
end;
tc4 := GetTickCount - tc4;
//-------------
ShowMessageFmt('Trunc: %d'#13'Trunc_Asm: %d'#13'Trunc_Dbl: %d'#13'Round: %d', [tc1, tc2, tc3, tc4]);
end;
__________________
Грамотно поставленный вопрос содержит не менее 50% ответа.
Грамотно поставленная речь вызывает уважение, а у некоторых даже зависть.
|