Board index » cppbuilder » Various sorting of TRichEdit->Lines - Is there any **faster** solution as mine...?
Oren Halvani
![]() CBuilder Developer |
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........ |