Re: Faster way of making long string?



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
>



.



Relevant Pages

  • Re: Formatted object properties with ToString() method
    ... the way you use a regular expression to derive a format string is nice. ... you need to know the class properties at design time and ... String.Formatargs arrays dynamically via reflection and benefit from the ... Just add the names of the properties in the array and in the Format call. ...
    (microsoft.public.dotnet.languages.csharp)
  • libintl.h not found
    ... cisco_ios.l:76: warning: Array size smaller than format string size ...
    (comp.unix.bsd.openbsd.misc)
  • Re: MSHFlexgrid displayed value
    ... data, one for my array, and one for the grid, which means using double the ... >> get the format string, and display the value to the user. ...
    (microsoft.public.vb.controls)
  • Re: Struggling with basics
    ... I've only just managed to examine the code, and the responses that people gave, and I'm now seriously struggling to understand why things aren't working correctly. ... not enough arguments for format string ... I have changed the clause that deletes the last array if the array count is 6 and seen what figures are being entered into the array. ... I'm pretty sure it's to do with comparing a string against an integer but can't for the life of me see where to force the comparrison to check against two integers. ...
    (comp.lang.python)
  • Re: Format String
    ... caveats) would be to create the format string dynamically as needed. ... each column in an array. ... > Error: incorect string format ...
    (microsoft.public.dotnet.languages.csharp)