Re: Faster way of making long string?




"Ian Hinson" <pparagon@xxxxxxxxxxxxxx> wrote in message
news:xyKae.22330$5F3.1324@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> I want to make a string of numbers from an integer list.
> The code I'm using is pretty simple solution.

As others have mentioned,
a) unless you are processing an awful lot of integers or doing it a huge
number of times the impact on your program of a better algorithm is likely
marginal, and
b) simply reducing the number of intermediate string variables involved
is going to produce the largest time savings.

Below is a solution that should be relatively quick. It uses a dynamic array
of integer, but can be easily modified for other data structures.

type
tIntList = array of integer;

function IntListToString (iList : tIntList) : string;

var p, dst : pChar; lth, ix : integer;

begin
lth := Length (iList) * 12 + 1; // largest possible size
p := StrAlloc (lth);
try
dst := p;
for ix := 0 to High (iList) do
inc (dst, FormatBuf (dst^, lth, '%d,', 3, [iList [ix]]));
dec (dst); // get rid of trailing comma
dst^ := #0;
result := p;
finally
StrDispose (p);
end;
end;


.



Relevant Pages

  • Re: string concatenation
    ... > from src to dst to form a string with strlen< size. ... _documented_ to act differently than the OpenBSD versions do. ... Need an efficient and powerful string library for C? ...
    (comp.lang.c)
  • Re: how to determine if files are on same or different file systems
    ... >> appropriate error handling. ... > That's why I'm looking for a way to tell if a particular partition is ... shutil.copy2(src, dst) ... Or use the length of the string and count backwards? ...
    (comp.lang.python)
  • Re: string concatenation
    ... > from src to dst to form a string with strlen< size. ... _documented_ to act differently than the OpenBSD versions do. ... Need an efficient and powerful string library for C? ...
    (comp.unix.programmer)
  • Re: Replace Space
    ... I thought it would be fun to mess with RegEx. ... within the string there are areas that may be defined with spaces ... Dim work As String, itm As String ... Dim src As Long, dst As Long, active As Boolean ...
    (microsoft.public.vb.general.discussion)
  • Re: VB6 to convert textfile data
    ... like this running through a VB6 conversion program and batch file setup but I ... Dim src As Long, dst As Long ... Private Function Adjusted(Text As String) As String ... Dim cnt As Long, list As Collection ...
    (microsoft.public.vb.general.discussion)