C99bb2b0dfcf7dba9aabf244a3330ac2

this function finds the first match in stringlist
where the value part of a "name=value" pair is equal value passed.

Any thoughts on speeding this up?

function indexOfValue(list : TStrings; value : string): integer;
 begin
  Result := 0;
  while (Result < list.Count) and (not SameText(list.ValueFromIndex[Result], value)) do
    Inc(Result);
  if Result >= list.Count then
    Result := -1;
 end;

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

Kenny

September 24, 2009, September 24, 2009 21:15, permalink

No rating. Login to rate!

The way you implemented this is identical to how TStrings.IndexOf handles it. Only your implementation has the overhead of additional function calls to look up TStrings.Count and TStrings.ValueFromIndex. Unless there are other characteristics to the TStrings list that haven't been mentioned (its sorted or only a specific subset of strings is allowed) you won't get better than Delphi's own implementation.

function indexOfValue(list : TStrings; value : string): integer;
begin
  Result := list.IndexOf(value);
end;
C99bb2b0dfcf7dba9aabf244a3330ac2

DarkAxi0m

September 25, 2009, September 25, 2009 03:50, permalink

No rating. Login to rate!

Sorry i should have made it more clear about the function, i will update the first post.

A4498a04928b7302fe4cc65133cd7bdd

atmotaIrrap

September 13, 2010, September 13, 2010 08:46, permalink

No rating. Login to rate!

I suck coz I only appear to be able to think of one joke and one joke alone...

Why did the plane crash into the house????

COZ THE LANDING LIGHT WAS ON!!

D41d8cd98f00b204e9800998ecf8427e

Tony

November 3, 2011, November 03, 2011 06:07, permalink

No rating. Login to rate!

I use functions like this too (I agree it's not the same as IndexOf, which matches the whole string) for improvement I'd do two things.

1. Use const to avoid copying the objects around
2. Use a for loop - for loops in delphi are highly optimised

function IndexOfValue(const List: TStrings; const Value: String): Integer;
  var
    I: Integer;
  begin
    Result := -1;
    for I := 0 to List.Count - 1 do
      if SameText(Value, List.ValueFromIndex[I]) then
        Exit(I);
  end;
D41d8cd98f00b204e9800998ecf8427e

Tony

November 3, 2011, November 03, 2011 06:07, permalink

No rating. Login to rate!

I use functions like this too (I agree it's not the same as IndexOf, which matches the whole string) for improvement I'd do two things.

1. Use const to avoid copying the objects around
2. Use a for loop - for loops in delphi are highly optimised

function IndexOfValue(const List: TStrings; const Value: String): Integer;
  var
    I: Integer;
  begin
    Result := -1;
    for I := 0 to List.Count - 1 do
      if SameText(Value, List.ValueFromIndex[I]) then
        Exit(I);
  end;

Your refactoring





Format Copy from initial code

or Cancel