Explode Function in Delphi

What do you know about "explode" in programming language?  Yes, it defines an additional set of parser functions that operate on strings. In some programming languages, there's native explode function found on their library - except for Delphi that didn't had it yet. So, most of Delphi programmers should build it manually. Anyway, I've been made a mini researches for manual Explode code in Delphi and need to find out what will work best - more effective and efficient.

There's 2 types of it, one with TArray constructor and others with TStrings constructor. Take a look at both codes below:

Explode with TArray

function Explode1(cDelimiter,  sValue : string; iCount : integer) : TArray;
var s : string; i,p : integer;
begin
s := sValue; i := 0;
while length(s) > 0 do
        begin
        inc(i);
        SetLength(result, i);
        p := pos(cDelimiter,s);
        if ( p > 0 ) and ( ( i < iCount ) OR ( iCount = 0) ) then
                begin
                result[i - 1] := copy(s,0,p-1);
                s := copy(s,p + length(cDelimiter),length(s));
                end
        else
                begin
                result[i - 1] := s;
                s :=  '';
                end;
        end;
end;

Explode with TStrings

function Explode2(const str: string; const separator: string): TStrings;
var     n: integer;
        p, q, s: PChar;
        item: string;
begin
Result := TStringList.Create;
try
        p := PChar(str);
        s := PChar(separator);
        n := Length(separator);
        repeat
                q := StrPos(p, s);
                if q = nil then q := StrScan(p, #0);
                SetString(item, p, q - p);
                Result.Add(item);
                p := q + n;
        until q^ = #0;
except
        item := '';
        Result.Free;
        raise;
        end;
end;




Test Scenario

So, here's the scenario: need to generate 10,000 lines of string into TMemo with delimiter at the end of each lines to explode. Each of explode types will read TMemo content and parsing it into TListBox component. To counting elapsed time, I put GetTickCOunt before explode function and after looping (parsing each explode result into TListBox). To give you additional visualization, here's time to generate 10,000 lines.



It takes 13.104 sec for one attempt. Take a look also on both parsing image below that potentially affect to the resource of the computer:



Since elapsed time result will different on each test attempt, so I made 3 times test to make sure that the test resulting in correct way.

Test #1



Test #2



Test #3



Conclusion

From the test, I can say that the best way to explode in Delphi is type #2 with some reasons:
  • Arrayless, means consuming low memory
  • Fastest speed elapsed time


So? Take your choice! :)

Labels:


PS: If you've benefit from this blog,
you can support it by making a small contribution.

Enter your email address to receive feed update from this blog:

Post a Comment

 

Post a Comment

Leave comments here...