Link to home
Start Free TrialLog in
Avatar of systan
systanFlag for Philippines

asked on

What is the fastest array in delphi

I have a File, a record type file or an untype file and during startup I want to load the 3 records into an array
Im not sure if:

--------------------------
var a1:Array of String //dynamic array
SetValue(a1, count+1);
a1[count]:= Myrecord1 //adds many records, about 900k
for count = ...
if a1[count] = 'TheOtherRecord' then ...
--------------------------

is faster than

------------------------
var a2:TstringList //hashed array
a2.Add(MyRecord1) //adds many records, about 900k
a2.Names(count)
for count = ...
if a2.Names(count) = 'TheOtherRecord' then ...
-----------------------


And I did not test it yet, because my records size is not enough to test, But if anyone of you has experience or a sure knowledged of fastest array in delphi, just answer what is it in your openion.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of cjcsoft
cjcsoft

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Emmanuel PASQUIER
first, this does not make much sense : "I have a File, a record type file or an untype file and during startup I want to load the 3 records into an array" .

second, you are comparing two different things, as Names property of TStringList will get you the 'name' of a name=value pair in your stringlist. So it has to extract the string before the '=' that **might** be there. I suppose that is not what you want. You should use the Items property instead, which is also the default array property of a TStringList.

for i:=0 to List.Count-1 do if List.Items[i]='something' Then ...
which can be also written like :
for i:=0 to List.Count-1 do if List[i]='something' Then ...

If you want to search a value in a StringList, easiest way is the use of IndexOf method :
i:=List.IndexOf('something');
if i>=0 Then ...

Note that TStringList is NOT a hashlist. But it can be a sorted list (if you don't care about the order of the values). If that is so (just set the Sorted property to true before adding values), IndexOf will work very fast. Otherwise, IndexOf will work pretty much the same as looking in all the values of an array.

In short, TStringList is ALWAYS a better option than a string array you manage yourself, because it has much more useful methods already implemented, and when used with the correct options for your needs, will be fast enough.
Avatar of systan

ASKER

Thank you

Actually, I don't need sorting or any properties in an array.
I just want to clarify that UNTYPED FILE (binary file?)is what I am trying to say.

This one:
:
Type TMyRec = Record
Name: String[15];
////Temporary Only
////MidInitial: Char;
////Age: Integer;
end;

////Global Variables
Const fn = 'UnTypedFile.Dat';
var mLength: Integer;
var PmyRec: TMyRec;
var MyStringArray: Array of String;
var ie: Integer;

procedure StartLoadRec;
var i: integer;
begin
i:=0;
mLength:=0;

 if FileExists(fn) then
    Stream := TFileStream.Create(fn, fmOpenReadWrite)
  else
    Stream := TFileStream.Create(fn, fmCreate);

    while stream.Position < stream.Size  do
    begin
    Stream.ReadBuffer(PmyRec, SizeOf(TmyRec));
    SetLength(MyStringArray, i);
    MyStringArray[i]:= PmyRec.Name;
    i:=i+1;
    end;

    Stream.Free;
    mLength := mLength + i;
end;


////Supposing PmyRec has 900 thousand records Loaded in MyStringArray
Procedure JustCompareEachOne(StringFromOther);
begin
for ie := 0 to (mLength -1) do
begin
////ThisString(StringFromOther) is Function Return Value of Some String
////Crypted is a Function incrypte/decrypte, because before saving the records, it is incrypted
if ThisString(StringFromOther) = Crypted(MyStringArray[ie]) then
begin
////AddToListBox is a procedure adding it to Listbox if are same
AddToListBox( Crypted(MyStringArray[ie]) );
end;
end;


procedure SaveRec(_JustSomeStringHere_);
begin
Stream := TFileStream.Create(fn, fmOpenWrite);
Stream.Position := Stream.Size;
PmyRec.Name := Crypted(_JustSomeStringHere_);
Stream.WriteBuffer(PmyRec,  SizeOf(TmyRec));
Stream.Free;
////Load to Memory - Again when Saving
SetLength(MyStringArray, mLength +1);
MyStringArray[mLength] := Crypted(_JustSomeStringHere_);
end;


//...
//Some codes here to complete the program ...
//...
//End.


NOW
Do you think <TStringList> is _Faster_ if I implement it on that same Code Structure ??
Or there is an <another> that is  _FasterThan_  both of them ??

Excute me for my connected question here:
If i Replace TFileStream with SqLite Database ( during Load/Read time of Recordss ),
Do you think UnTyped-File is  _FasterThan_  SqLite on Reading recordss like my sample above?
basically, even the best DataBase have to READ data in a file at some point. So NO, nothing can be quicker than direct file loading.

At this point of your code, I don't see why you bother with TFileStream instead of plain old typed files (see AssignFile, Reset , Read, CloseFile in Delphi Help).

Your JustCompareEachOne is so cryptic that we can't help you much. I'm just wondering why you are calling Crypted on your strings again and again. Why not just do it once and for all on all strings in StartLoadRec ? If you are so much after performance....

As for TStringList, it's very convenient if you have a need to store objects related with a unique string key, and find them quickly. But if you are trying to find those objects based on some calculated value, it's not going to help much. Still, it couldn't hurt more than a basic array.
Avatar of systan

ASKER

>Do you think <TStringList> is _Faster_ if I implement it on that same Code Structure ??
So your answer is NO!, meaning [ array of string is faster ] in my code structure.

>Or there is an <another> that is  _FasterThan_  both of them ??  
Nothing or no comment?  Ok

>If i Replace TFileStream with SqLite Database ( during Load/Read time of Recordss ),
>Do you think UnTyped-File is  _FasterThan_  SqLite on Reading recordss like my sample above?
So NO, nothing can be quicker than direct file loading.

Thank you,
I'll see your advice about cryted
> So your answer is NO!, meaning [ array of string is faster ] in my code structure.

No, that's not what I said. if you want to search a list of strings or string+object (see AddObject method of TStringList) , using the string as key, then :
a) if you can afford having your list sorted, then IndexOf will be a lot quicker than anything else, even if you don't actually NEED it to be sorted that is what you SHOULD do.
b) if you can't afford sorting the list because you NEED it to keep its natural order, then IndexOf will be only so little slower that it won't be a problem, considering all the great features you can implement easily using TStringList instead of a plain array.

Your needs are not yet clear enough so that I can give you anything else than global advices. TStringList seems to be what you need, now, and with next requirements.
Avatar of systan

ASKER

Ok,   I got it,   but my code structure is not searching a list of strings,   and I don't want to sort it,   because if you look at my code structure,   you will find just <comparing strings>.    Thats what I ask in details, which is faster when comparing strings only,  is it the variable data type <array of strings> or the data type <TstringList>

But now your advice  is to use <TstringList>, but you mean if I'm going use it with sorting or searching.


And cJcSoft answer is to use <array of strings> and showed some disadvantages of <TstringList>


Thank you to both of you
I'll think what is best, really  (in comparing strings) or even adding thousands of  strings in memory sequencially).
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what is missing from this problem is the context.  Your question asks which of two configurations is faster, but doesn't ask what is the fastest possible method to manage X data with Y volume and Z processing operations.

I suggest you look at the speed and functionality of TClientDataset, using the CDS (binary) format for loading your data.

By the way...is your string data simple ASCII characters (<127) or more complex strings?
Avatar of systan

ASKER

@epasquier, Thank you for the clarification and advice

@aikimark, Yes it only has ascii simple characters, but what do you mean <127 ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of systan

ASKER

Ok, do you mean by setting < array of PChar >  instead of < array of String > would make a difference in speed?
You have only hinted at the data structure you need to store, so I'm not going to suggest anything until I know more.  It is possible that you have a very complex data structure and not just an iterative string/pchar set.

We don't know what kind of processing you require once you have read the text from the hard drive.

Context is king to understanding your problem.
Avatar of systan

ASKER

ID: 30849136
If you've read my code structure above, that's the only important of my code, loading and comparing strings, no sorting, no searching.
@systan

You are presenting us with a partial view of code you have written.  What are you trying to DO with the data?  What kind of data will you have in production?
Avatar of systan

ASKER

Actually that are my test, I'm just knowing delphi, thats all, Thanks.
you are learning to program with Delphi (Object Pascal)?
Avatar of systan

ASKER

ah, Ok, thanks.