Board index » cppbuilder » Various sorting of TRichEdit->Lines - Is there any **faster** solution as mine...?

Various sorting of TRichEdit->Lines - Is there any **faster** solution as mine...?


2005-08-29 09:49:20 PM
cppbuilder5
dear builders,
i've got a filter programm that really need to handle files
(database-textfiles) with a
size-average of: 1,5 - 2 MByte each...
with the below functions i need 5-6 min (and more..) on a P4 with 2,0 GH'z
and 512 MByte Ram (!!!)
i believe the slow time-duration doesn't have to do something with my
current hardware-configuration,
because i've tested the programm on several machines with diffrent
hardware-configuration, i get ~ nearly
the same time duration for the results...
is there something that i can optimize....?
- does the VCL controll itself ( TRichEdit )caising the slow processing of
the sorting...?
- will the STL help me in this case...? or do the both use the same
algorythm (QuickSort) to sort...?
- can i add the strings into something else as VCL's StringList, for example
a vector...?
or woulndn't that make sense.....?
---->sList->AddStrings(rePreview->Lines); // i guess this is also causing
the slow processing...right ?
Oren
PS: i use BCB 3, thats why CustomSort(..) is declared here...
/*************************************************************************/
// the algorythm...
typedef int __fastcall (*TStringListSortCompare) (TStringList *List, int
Index1, int Index2);
void __fastcall StringListQuickSort(TStringList *List, int L, int R,
TStringListSortCompare SCompare)
{
int I, J, P;
do
{
I = L; J = R; P = ((L + R)>>1);
do
{
while(SCompare(List, I, P) < 0) ++I;
while(SCompare(List, J, P)>0) --J;
if(I <= J)
{
List->Exchange(I, J);
if(P == I) P = J; else if(P == J) P = I; ++I; --J;
}
}
while(I <= J);
if(L < J) StringListQuickSort(List, L, J, SCompare); L = I;
}
while(I < R);
}
void __fastcall CustomSort(TStringList *List, TStringListSortCompare
SCompare)
{
StringListQuickSort(List, 0, List->Count-1, SCompare);
}
/********************* Callbacks...
*****************************************/
int __fastcall AlphaCallback(TStringList *List, int Index1, int Index2)
{
AnsiString Line1 = List->Strings[Index1], Line2 = List->Strings[Index2];
int multiplier;
if(frmMainUnit->cmdSortUp->Down) multiplier = 1;
else multiplier = -1;
return (AnsiCompareStr(Line1, Line2) * multiplier);
}
int __fastcall LengthCallback(TStringList *List, int Index1, int Index2)
{
AnsiString Line1 = List->Strings[Index1], Line2 = List->Strings[Index2];
int multiplier;
if(frmMainUnit->cmdSortUp->Down) multiplier = 1;
else multiplier = -1;
return ((Line1.Length() - Line2.Length()) * multiplier);
}
//...
//....MANY OTHERS........
//...
void __fastcall TfrmMainUnit::WhichSort(SortType whichType)
{
static TStringListSortCompare Callbacks[] = { NULL, AlphaCallback,
LengthCallback };
TStringList *sList = new TStringList;
try
{
Screen->Cursor = crHourGlass;
try
{
sList->Duplicates = dupAccept;
sList->AddStrings(rePreview->Lines);
if(whichType != sort_None) CustomSort(sList, Callbacks[whichType]);
rePreview->Lines->Assign(sList);
}
__finally
{
Screen->Cursor = crDefault;
}
}
__finally
{
delete sList;
}
}
void __fastcall TfrmMainUnit::cmdSort_AlphaClick(TObject *Sender)
{
WhichSort(sort_Alpha);
rePreview->SelStart = 0;
rePreview->SetFocus();
}
void __fastcall TfrmMainUnit::cmdSort_LineLenClick(TObject *Sender)
{
WhichSort(sort_Lines);
rePreview->SelStart = 0;
rePreview->SetFocus();
}
//...
//....MANY OTHERS........
 
 

Re:Various sorting of TRichEdit->Lines - Is there any **faster** solution as mine...?

Oren Halvani wrote:
Quote
dear builders,

i've got a filter programm that really need to handle files
(database-textfiles) with a
size-average of: 1,5 - 2 MByte each...

with the below functions i need 5-6 min (and more..) on a P4 with 2,0 GH'z
and 512 MByte Ram (!!!)

i believe the slow time-duration doesn't have to do something with my
current hardware-configuration,
because i've tested the programm on several machines with diffrent
hardware-configuration, i get ~ nearly
the same time duration for the results...

is there something that i can optimize....?

- does the VCL controll itself ( TRichEdit )caising the slow processing of
the sorting...?
In the code below you sort a seperate list so richedit won't be the problem.
- will the STL help me in this case...? or do the both use the same
algorythm (QuickSort) to sort...?
The std::sort is in most cases a quicksort. Ofcourse the implementation
might be a little bit more efficient then yours but I wouldn't expect
miracles.
Quote
- can i add the strings into something else as VCL's StringList, for example
a vector...?
or woulndn't that make sense.....?
It depends. When the strings are large swapping of two strings in a
vector might become slow if they are small this is not a problem.
Remember you do a lot of swapping in quicksort.
A vector os AnsiStrings is more efficient memory wise.
Quote

---->sList->AddStrings(rePreview->Lines); // i guess this is also causing
the slow processing...right ?
Depends on the number of strings. Raw copying of the data shouldn't be
the issue but allocating thousands of memory blocks is.
Quote

Oren

[snip]
int __fastcall AlphaCallback(TStringList *List, int Index1, int Index2)
{
AnsiString Line1 = List->Strings[Index1], Line2 = List->Strings[Index2];
int multiplier;

if(frmMainUnit->cmdSortUp->Down) multiplier = 1;
else multiplier = -1;

return (AnsiCompareStr(Line1, Line2) * multiplier);
}

int __fastcall LengthCallback(TStringList *List, int Index1, int Index2)
{
AnsiString Line1 = List->Strings[Index1], Line2 = List->Strings[Index2];
int multiplier;

if(frmMainUnit->cmdSortUp->Down) multiplier = 1;
else multiplier = -1;

return ((Line1.Length() - Line2.Length()) * multiplier);
}
The performance of the comparision functions is very important with
quicksort because it is the operation which is performed most often.
Because of this I have two tips. First remove the local copies of the
strings and use directly the strings in the list. Second remove the if
statements instead calculate the multiplier once outside the function
and use the value inside the compare functions.
Eelke
 

Re:Various sorting of TRichEdit->Lines - Is there any **faster** solution as mine...?

"Eelke" < XXXX@XXXXX.COM >wrote in message news:431459ee$ XXXX@XXXXX.COM ...
Quote
The performance of the comparision functions is very important with quicksort because it is the operation which is performed most
often.
Because of this I have two tips. First remove the local copies of the strings and use directly the strings in the list. Second
remove the if statements instead calculate the multiplier once outside the function and use the value inside the compare
functions.

Eelke
thanks Eelke,
i'll see if these hints taking effect on the speed...
Oren
 

{smallsort}