Re: Faster way of making long string?



Hello again,

Ian Hinson wrote:
> I want to make a string of numbers from an integer list.
> The code I'm using is pretty simple solution.

first my apologies: I did not tested the suggested code, and at lest in D4 it is awful sloooow!!!

I made a test (source below) and the result for D4 on my poor computer is 57ms to 32s (!).

So be lucky to have a newer delphi and use the suggested fast routine :-)

> Thanks,
> Ian.
Best regards
Ekkehard Domning

To the source
Place a button and two labels, patch the code and have a test.

unit StringAddingMain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    IntegerList : array of Integer;
    ResultString : String;
    procedure FastIntegerListToString;
    procedure StringIntegerListToString;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
procedure TForm1.FastIntegerListToString;
var i,l,p : Integer;
    NumString : ShortString;
begin
  l := 0;
  // First calculate the length of the resultstring
  // this ist the sum from the length each number has
  for i := 0 to High(IntegerList) do
    begin
      case IntegerList[i] of
        0..9                 : l := l + 1;
        10..99               : l := l + 2;
        100..999             : l := l + 3;
        1000..9999           : l := l + 4;
        10000..99999         : l := l + 5;
        100000..999999       : l := l + 6;
        1000000..9999999     : l := l + 7;
        10000000..99999999   : l := l + 8;
        100000000..999999999 : l := l + 9;
        1000000000..MaxInt   : l := l + 10;
      end;
    end;
  // plus the amount of commas needed
  l := l + High(IntegerList);
  //second create the string
  SetLength(ResultString,l);
  p := 1; //Position in the string
  for i := 0 to High(IntegerList) do
    begin
      if i > 0 then //Patch comma, but not before the first string
        begin
          ResultString[p] := ',';
          Inc(p);
        end;
      NumString := IntToStr(IntegerList[i]);
      l := Length(NumString);
      Move(NumString[1],ResultString[p],l);
      p := p + l;
    end;
end;
procedure TForm1.StringIntegerListToString;
var sl : TStringList;
    i : Integer;
begin
  sl := TStringList.Create;
  try
    for i := 0 to High(IntegerList) do
      sl.Add(IntToStr(IntegerList[i]));
    ResultString := sl.CommaText;
  finally
    sl.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i : Integer;
    StartTime, StopTime : Cardinal;
begin
  SetLength(IntegerList,16384);
  for i := 0 to High(IntegerList) do
    IntegerList[i] := Random(MaxInt);
  StartTime := GetTickCount;
  FastIntegerListToString;  //Run the fast Test
  StopTime := GetTickCount;
  Label1.Caption := IntToStr(StopTime-StartTime);
  StartTime := GetTickCount;
  StringIntegerListToString; //Run the Stringlisttest
  StopTime := GetTickCount;
  Label2.Caption := IntToStr(StopTime-StartTime);
end;

end.
.


Quantcast