At the end of the day, the only way to make it faster is to pre-allocate the result string, and then treat it as a buffer using pchar's - I use this method quite often.
 
Your function is quite good, but still results in ItemCount string allocations - hence ItemCount calls to the memory manager - which is the slow part in using dynamic (long) strings.
 
You could probably make the assumption in dealing with sql column names, that each name could take upto 32 characters (Max 31 plus coma), then double just to be safe, eg Setlength( S, ItemCount*64) And on exit just make the length shorter by using SetLength again.
 
Also you dont want to make multiple function calls, so call Sep once and store it in a local variable, and of course if you know that it will be only 1 or 2 chars long, you could use a Character array stored locally on stack instead - again avoiding memory manager calls.
 
Given that you are doing a simple concat, using Format is a bit of overkill - it does execute a lot of code and so does take some time. You might even find doing S := S + ',' + Item(i) might be faster - as of course mentioned yesterday, execute your method several million times and see how long different versions take - at the end of the day it is the best (only?) real test.
 
Myles.
 
-----Original Message-----
From: Neven MacEwan [mailto:[EMAIL PROTECTED]]
Sent: Friday, 1 November 2002 12:28 a.m.
To: Multiple recipients of list delphi
Subject: [DUG]: Friday Challange

Hi all,
 
Further to my enpty string question, I'm writing a lot of
functions that return a set of strings joined by a separator
ie 'col1, col2,...' or 'col1 = 'a' and colb = 'b'' (as you may guess
these are all parts of SQL Statements)
 
given a function 'Itemcount' that returns the number of items
and item(i) that returns the item string, and function sep what is the
best form of such a function
 
to seed the duscussion I'll give you one of my variants
 
function statement: string;
var
  I: integer;
begin
  Result := '';
  for I := 0 to pred(ItemCount) do
    if I = 0 then Result := Item(I)
    else Result := format('%s%s%s',[Result,Sep,Item(I)])
end;
 
Variants and explainations pls
 
Neven
 

Reply via email to