Here is a faster algorithm avoiding a division:
function CountDigits(anInt: Cardinal): Cardinal; inline;
var
cmp: Cardinal;
begin
cmp := 10;
Result := 1;
while (Result < 10) and (cmp <= anInt) do
begin
cmp := cmp*10;
Inc(Result);
end;
end;
function CountDigitsAsm(anInt: Cardinal): Cardinal;
asm
mov ecx,$a // cmp := 10;
mov edx,$1 // Result := 1;
jmp @loop2
cmp eax,edx // while cmp <= anInt do
jb @done
@loop1:
add ecx,ecx // cmp := cmp*10;
lea ecx,[ecx+ecx*4]
inc edx // Inc(Result);
@loop2:
cmp edx,$0a // (Result < 10)
jnb @done
cmp eax,ecx
jnb @loop1
@done:
mov eax,edx
end;
begin
WriteLn(CountDigitsAsm(10));
WriteLn(CountDigitsAsm(99));
WriteLn(CountDigitsAsm(999));
WriteLn(CountDigitsAsm(9999));
WriteLn(CountDigitsAsm(99999));
ReadLn;
end.
Note that the pas version can be inlined and could possibly be faster than the asm version.
Ok, here is a lut (lookup table) solution to avoid any multiplication:
function CountDigitsLUT(anInt: Cardinal): Cardinal; inline;
const
lut: array[1..10] of cardinal =
(9,
99,
999,
9999,
99999,
999999,
9999999,
99999999,
999999999,
$FFFFFFFF);
begin
Result := 1;
while anInt > lut[Result] do
Inc(Result);
end;
And an unrolled version:
function CountDigitsUnrolled(anInt: Cardinal): Cardinal; inline;
begin
if (anInt < 10) then Result := 1 else
if (anInt < 100) then Result := 2 else
if (anInt < 1000) then Result := 3 else
if (anInt < 10000) then Result := 4 else
if (anInt < 100000) then Result := 5 else
if (anInt < 1000000) then Result := 6 else
if (anInt < 10000000) then Result := 7 else
if (anInt < 100000000) then Result := 8 else
if (anInt < 1000000000) then Result := 9 else
Result := 10;
end;
And @TLama's case contribution:
function CountDigitsCase(Value: Cardinal): Cardinal; inline;
begin
case Value 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;
else
Result := 10;
end;
end;
Timing the different solutions:
Unrolled: 4097 ms
Case: 1444 ms
LUT: 3233 ms
pas: 6199 ms
asm: 6747 ms
Test code:
sw := TStopWatch.StartNew;
for i := 1 to 1000000000 do
j := CountDigitsXXX(i);
WriteLn(sw.ElapsedMilliseconds,' ',j);
Addendum
Inspired by this answer,
here is a Delphi implementation which is an O(1) solution:
function OpenBit(AValue: Cardinal): Cardinal; register;
asm // Highest bit set
BSR EAX, EAX
end;
function CountDigitsO1(value: Cardinal): Cardinal; inline;
const
Powers: array[0..9] of Cardinal = (
0,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000);
MaxDigits: array[0..32] of cardinal =
(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10);
begin
Result := MaxDigits[OpenBit(value)];
if (value < Powers[Result-1]) then
Dec(Result);
end;
Compared to the CountDigitsCase()
it has a more even time distribution in finding the solution for a given digit. But still overall a bit slower (on my machine).
Digit Case O1
------------------
1 0.930 2.200 nanoseconds per call
2 0.922 1.689
3 0.944 1.500
4 1.078 1.578
5 1.878 1.522
6 1.200 1.667
7 1.356 1.567
8 1.356 1.522
9 1.502 1.664
10 1.246 1.761
Test code:
procedure TestXXX(var Distribution: array of Double);
var
sw: TStopWatch;
i,j,k,m: Cardinal;
const
StartIx: array[0..9] of Cardinal = ( 0,10,100,1000,10000,100000,1000000,
10000000,100000000,100000000);
StopIx: array[0..9] of Cardinal = ( 9,99,999,9999,99999,999999,9999999,
99999999,999999999,$FFFFFFFF);
Repeats: array[0..9] of Cardinal = (10000000,1000000,100000,10000,1000,100,10,1,1,1);
begin
for k := 0 to 9 do begin
sw := TStopWatch.StartNew;
for m := 1 to Repeats[k] do
for i := StartIx[k] to StopIx[k] do
j := CountDigitsXXX(i);
Distribution[k] := sw.ElapsedMilliseconds*1000000.0/(1.0*Repeats[k]*(StopIx[k]- StartIx[k] + 1));
WriteLn(sw.ElapsedMilliSeconds,' ',j);
end;
end;