function howManyNumbers(n : Int64): Int64;
begin
if n < 100000 then
begin
if n < 100 then
begin
if n < 10 then
begin
result := 1; exit;
end
else
begin
result := 2; exit;
end;
end
else
begin
if n < 1000 then
begin
result := 3; exit;
end
else
if n < 10000 then
begin
result := 4; exit;
end
else
begin
result := 5; exit;
end;
end;
end
else
begin
if n < 10000000 then
begin
if n < 1000000 then
begin
result := 6; exit;
end
else
begin
result := 7; exit;
end
end
else
begin
if n < 100000000 then
begin
result := 8; exit;
end
else
begin
if n < 1000000000 then
begin
result := 9; exit;
end
else
begin
result := 10; exit;
end;
end;
end;
end;
end;
VAR {global}
TB1 : array[0..9] of integer = (5,18,556,24,8,4000,555,7777,45,8546);
TB2 : array[0..9] of integer;
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
for i := Low(TB1) to High(TB1) do TB2[i] := length(inttostr(TB1[i]));
end;
{ la fonction à f0xi, je l'ai mise en forme pour mieux l'analyser, je suis un peu maniaque ^^ } function howManyNumbers(n: Int64): Byte; begin if n < 100000 then begin if n < 100 then begin if n < 10 then begin Result := 1; Exit; end else begin Result := 2; Exit; end; end else begin if n < 1000 then begin Result := 3; Exit; end else if n < 10000 then begin Result := 4; Exit; end else begin Result := 5; Exit; end; end; end else begin if n < 10000000 then begin if n < 1000000 then begin Result := 6; Exit; end else begin Result := 7; Exit; end end else begin if n < 100000000 then begin Result := 8; Exit; end else begin if n < 1000000000 then begin Result := 9; Exit; end else begin Result := 10; Exit; // <- utile ? end; end; end; end; end; { ma fonction } function GetLengthNumber(Number: Int64): Byte; begin case Number 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; //****************************************************************************** var arrNumbers: array of Integer; // On remplit de manière aléatoire le tableau qui servira à tester les 2 méthodes : procedure FillArray; const LENGTH_ARRAY = 100000; // longueur du tableau const NumberWidth: array[0..9] of Integer = (10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, High(Integer)); var i: Integer; begin Randomize; SetLength(arrNumbers, LENGTH_ARRAY); for i := 0 to High(arrNumbers) do arrNumbers[i] := Random(NumberWidth[Random(Length(NumberWidth))]); end; //****************************************************************************** procedure TForm1.FormCreate(Sender: TObject); var i: Integer; begin // Mise en forme de la fiche : Caption := 'Longueur d''un nombre'; Button1.Width := 141; Button1.Caption := 'howManyNumbers'; Button2.Width := 141; Button2.Caption := 'Length(IntToStr(n))'; Button3.Width := 141; Button3.Caption := 'GetLengthNumber'; Label1.Caption := '0'; // Création du tableau de nombres : FillArray; // Affichage du tableau dans la ListBox1 : ListBox1.Clear; ListBox1.Items.BeginUpdate; for i := 0 to High(arrNumbers) do ListBox1.Items.Append(IntToStr(arrNumbers[i])); ListBox1.Items.EndUpdate; end; const // on peut augmenter cette valeur (ou la longueur du tableau) pour plus de précision : LOOP_MAXI = 100; procedure TForm1.Button1Click(Sender: TObject); var i, j: Integer; arrResults: array of Byte; TickCount: DWORD; begin SetLength(arrResults, Length(arrNumbers)); TickCount := GetTickCount; for j := 0 to LOOP_MAXI do for i := Low(arrNumbers) to High(arrNumbers) do arrResults[i] := howManyNumbers(arrNumbers[i]); TickCount := GetTickCount - TickCount; // Affichage du temps écoulé : Label1.Caption := IntToStr(TickCount); // Visualisation du résultat dans ListBox2 ListBox2.Clear; ListBox2.Items.BeginUpdate; for i := Low(arrResults) to High(arrResults) do ListBox2.Items.Append(IntToStr(arrResults[i])); ListBox2.Items.EndUpdate; end; procedure TForm1.Button2Click(Sender: TObject); var i, j: Integer; arrResults: array of Byte; TickCount: DWORD; begin SetLength(arrResults, Length(arrNumbers)); TickCount := GetTickCount; for j := 0 to LOOP_MAXI do for i := Low(arrNumbers) to High(arrNumbers) do arrResults[i] := Length(IntToStr(arrNumbers[i])); TickCount := GetTickCount - TickCount; // Affichage du temps écoulé : Label1.Caption := IntToStr(TickCount); // Visualisation du résultat dans ListBox2 ListBox2.Clear; ListBox2.Items.BeginUpdate; for i := Low(arrResults) to High(arrResults) do ListBox2.Items.Append(IntToStr(arrResults[i])); ListBox2.Items.EndUpdate; end; procedure TForm1.Button3Click(Sender: TObject); var i, j: Integer; arrResults: array of Byte; TickCount: DWORD; begin SetLength(arrResults, Length(arrNumbers)); TickCount := GetTickCount; for j := 0 to LOOP_MAXI do for i := Low(arrNumbers) to High(arrNumbers) do arrResults[i] := GetLengthNumber(arrNumbers[i]); TickCount := GetTickCount - TickCount; // Affichage du temps écoulé : Label1.Caption := IntToStr(TickCount); // Visualisation du résultat dans ListBox2 ListBox2.Clear; ListBox2.Items.BeginUpdate; for i := Low(arrResults) to High(arrResults) do ListBox2.Items.Append(IntToStr(arrResults[i])); ListBox2.Items.EndUpdate; end;