Hello Arno, On Tue, Dec 7, 2010 at 1:57 PM, Arno Garrels <arno.garr...@gmx.de> wrote:
> Fastream Technologies wrote: > > As far as I know, the TStringList->AddObject() method uses binary > > search for finding the correct index to insert when > > TStringList->Sorted = true. I don't know why I thought so but there > > is the method, > > Yes, it actually uses function QuickSort but that is still much slower > than a simple Add() of an unsorted list. So in order to compare speed > one has to messure both Add() and IndexOf(). > In most cases IndexOf() is not used frequently if not just once > when the client disconnects from the server. > > -- > Arno Garrels > > -- > To unsubscribe or change your settings for TWSocket mailing list > please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket > Visit our website at http://www.overbyte.be > This is the new function I wrote: function seekIndexToInsertForSortedStringList(stringList: TStringList; stringToAdd: String; caseinsensitive: boolean) : Integer; var low, mid, high: integer; begin if(stringList.Count = 0) then begin Result := 0; Exit; end; low := 0; high := stringList.Count - 1; if(caseinsensitive) then begin if(AnsiCompareStr(stringList.Strings[low], stringToAdd) > 0) then result := 0 else if(AnsiCompareStr(stringList.Strings[high], stringToAdd) < 0) then result := high + 1 else begin while(low < high) do begin mid := low + ((high - low) div 2); // 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 and if want to add 0.5, 1.5 and 2.5 as well as 7.0 if(AnsiCompareStr(stringList.Strings[mid], stringToAdd) < 0) then low := mid else // can't be high = mid-1: here A[mid] >= value, // so high can't be < mid if A[mid] == value high := mid; end; // high == low, using high or low depends on taste if(low <= stringList.Count - 1) then begin // found if((low = 0) or (AnsiCompareStr(stringList.Strings[low], stringToAdd) > 0)) then result := low else result := low - 1; end else result := -1; // not found end; end else begin if(stringList.Strings[low] > stringToAdd) then result := 0 else if(stringList.Strings[high] < stringToAdd) then result := high + 1 else begin while(low < high) do begin mid := low + ((high - low) div 2); if(stringList.Strings[mid] < stringToAdd) then low := mid + 1 else // can't be high = mid-1: here A[mid] >= value, // so high can't be < mid if A[mid] == value high := mid; end; // high == low, using high or low depends on taste if(low <= stringList.Count - 1) then begin // found if((low = 0) or (stringList.Strings[low] > stringToAdd)) then result := low else result := low - 1; end else result := -1; // not found end; end; end; To insert to an actually sorted string, you must NOT set Sorted = true; and also Duplicates should be dupAccept for the maximum speed and call the insert as, function TCustomWSocketMTServer.AfterClientCreated( ... clientString := IntToStr(ULong(Client)); FClientList.InsertObject(seekIndexToInsertForSortedStringList(FClientList, clientString, false), clientString, Client); It seems to work perfectly! Looking forward to your feedback, SZ -- To unsubscribe or change your settings for TWSocket mailing list please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be