Re: Faster way of making long string?
- From: "Ian Hinson" <pparagon@xxxxxxxxxxxxxx>
- Date: Mon, 25 Apr 2005 20:16:43 GMT
Hi Alan,
I tested your suggestion with a format string for 16 numbers by modifying
Chris Willig's code as follows:
const
NUMCOUNT = 16384; // size of array for test numbers
BUFSIZE = 256; // buffer size for each chunk of text
fmtstr = '%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d,
';
buf_sz = 200;
var
i, n, len, j : integer;
strOut : string;
buf: array [0..BUFSIZE-1] of char;
nums: array [0..NUMCOUNT-1] of integer;
fmtlen: integer;
finalfmt: array [0..63] of char; // for chopped down fmtstr last loop
k: integer;
v: array of TVarRec;
begin
{ Initialise array with some numbers }
for i := 0 to NUMCOUNT - 1 do nums[i] := i;
fmtlen := Length(fmtstr); // value used by FormatBuf
j := buf_sz;
SetLength(strOut, j);
n := 0;
i := 0;
while i < NUMCOUNT do
begin
if (NUMCOUNT - i) > 16 then
begin
len := FormatBuf(buf, BUFSIZE, fmtstr, fmtlen,
[nums[i], nums[i+1], nums[i+2], nums[i+3], nums[i+4],
nums[i+5],
nums[i+6], nums[i+7], nums[i+8], nums[i+9], nums[i+10],
nums[i+11],
nums[i+12], nums[i+13], nums[i+14], nums[i+15]]);
i := i + 16;
end
else
begin { output the final 16 or less numbers }
fmtlen := 4 * (NUMCOUNT - i) - 2;
// truncate format string to length for remaining numbers
StrLCopy(finalfmt, fmtstr, fmtlen);
{ set up a value capable of being passed to FormatBuf's sarray of const
parameter }
SetLength(v, NUMCOUNT - i);
for k := 0 to NUMCOUNT - i - 1 do
begin
v[k].VType := vtInteger;
v[k].VInteger := nums[i+k];
end;
len := FormatBuf(buf, BUFSIZE, finalfmt, fmtlen, v);
i := NUMCOUNT; // causes loop to exit after this iteration
end;
if j < (n + len) then
begin
Inc(j, len + buf_sz);
SetLength(strOut, j);
end;
MoveMemory(@strOut[n +1], @buf, len);
Inc(n, len);
end;
SetLength(strOut, n);
end;
Even with formatting just 16 numbers at a time, it made a significant
improvement.
eg. on my Celeron 1.3GHz it reduced the time taken from 23mS (using
IntToStr) to in the range 3-5 mS.
So, in order to get a clearer comparison, I dusted off my old Pentium 120
laptop (pre-MMX).
The results were ~95mS (FormatBuf) Vs ~340mS (IntToStr).
Ian.
<alanglloyd@xxxxxxx> wrote in message
news:1114438978.745115.62250@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>I think if you did the conversion using Format() (in SysUtils.pas) in
> batches of 100 or more in an array you'd find it even quicker. You
> could make the format string once and re-use it (%d,%d,%d,%d,%d, etc
> etc to 100 or more) AFAICS it allocates a long string as a buffer and
> puts the converted chars into that.
>
> Alan Lloyd
>
.
- Follow-Ups:
- Re: Faster way of making long string?
- From: alanglloyd
- Re: Faster way of making long string?
- References:
- Faster way of making long string?
- From: Ian Hinson
- Re: Faster way of making long string?
- From: Chris Willig
- Re: Faster way of making long string?
- From: Ian Hinson
- Re: Faster way of making long string?
- From: alanglloyd
- Faster way of making long string?
- Prev by Date: Re: Faster way of making long string?
- Next by Date: Re: Faster way of making long string?
- Previous by thread: Re: Faster way of making long string?
- Next by thread: Re: Faster way of making long string?
- Index(es):
Relevant Pages
|