program Project1;
{$mode objfpc}{$H+}
uses sysutils,Math;
function DecLength(i: DWord): SizeUInt;inline;
const
CTable: array [0..31] of QWord = (
4294967296, 8589934582, 8589934582, 8589934582,
12884901788, 12884901788, 12884901788, 17179868184,
17179868184, 17179868184, 21474826480, 21474826480,
21474826480, 21474826480, 25769703776, 25769703776,
25769703776, 30063771072, 30063771072, 30063771072,
34349738368, 34349738368, 34349738368, 34349738368,
38554705664, 38554705664, 38554705664, 41949672960,
41949672960, 41949672960, 42949672960, 42949672960
);
begin
if i = 0 then
Result:= 1
else
Result:= (i + CTable[Trunc(Log2(i))]) shr 32;
end;
function IntLog10(v: DWord): SizeUInt; inline;
begin
Result:=0;
While v>=10 do
begin
Inc(Result);
v := v div 10;
end;
end;
function TruncLog10(v: DWord): SizeUInt; inline;
begin
if v=0 then
Result:=0
else
Result:=trunc(log10(v));
end;
function StrLen(v: DWord): SizeUInt; inline;
begin
Result:=Length(IntToStr(v));
end;
function LookUp(v: DWord): SizeInt; inline;
const
LATable: array [2..10] of QWord = (10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000);
begin
for Result:=High(LATable) downto Low(LATable) do
if v >= LATable[Result] then
Exit;
Result:=1;
end;
function Hardcode(v: DWord): SizeInt; inline;
begin
case v of
0..9: Result:=1;
10..99: Result:= 2;
100..999: Result:=3;
1000..9999: Result:=4;
10000..99999: Result:=5;
100000..999999: Result:=6;
1000000..9999999: Result:=7;
10000000..99999999: Result:=8;
100000000..999999999: Result:=9;
otherwise Result:=10;
end;
end;
const
laps=100000000;
var
s: QWord;
x, i: Integer;
begin
x:=0;
s:=GetTickCount64;
for i:=1 to laps do
x:=x+DecLength(Random(100000000));
WriteLn('DecLength: ', GetTickCount64-s);
s:=GetTickCount64;
for i:=1 to laps do
x:=x+IntLog10(Random(100000000)) + 1;
WriteLn('IntLog10: ', GetTickCount64-s);
s:=GetTickCount64;
for i:=1 to laps do
x:=x+TruncLog10(Random(100000000)) + 1;
WriteLn('TruncLog10: ', GetTickCount64-s);
s:=GetTickCount64;
for i:=1 to laps do
x:=x+StrLen(Random(100000000));
WriteLn('StrLen: ', GetTickCount64-s);
s:=GetTickCount64;
for i:=1 to laps do
x:=x+LookUp(Random(100000000));
WriteLn('LookUp: ', GetTickCount64-s);
s:=GetTickCount64;
for i:=1 to laps do
x:=x+Hardcode(Random(100000000));
WriteLn('Hardcode: ', GetTickCount64-s);
end.