Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1062843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:41:30+00:00 2026-05-16T18:41:30+00:00

I have to check if I have duplicate paths in a FileListBox (FileListBox has

  • 0

I have to check if I have duplicate paths in a FileListBox (FileListBox has the role of some kind of job list or play list).
Using Delphi’s SameText, CompareStr, CompareText, takes 6 seconds. So I came with my own compare function which is (just) a bit faster but not fast enough. Any ideas how to improve it?

function SameFile(CONST Path1, Path2: string): Boolean;
VAR i: Integer;
begin
 Result:= Length(Path1)= Length(Path2);                                         { if they have different lenghts then obviously are not the same file }
 if Result then
  for i:= Length(Path1) downto 1 DO                                             { start from the end because it is more likely to find the difference there }
   if Path1[i]<> Path2[i] then
    begin
     Result:= FALSE;
     Break;
    end;
end;

I use it like this:

 for x:= JList.Count-1 downto 1 DO
  begin
   sMaster:= JList.Items[x];
   for y:= x-1 downto 0 DO
    if SameFile(sMaster, JList.Items[y]) then
     begin
      JList.Items.Delete (x); { REMOVE DUPLICATES }
      Break;
     end;
  end;

Note: The chance of having duplicates is small so Delete is not called often. Also the list cannot be sorted because the items are added by user and sometimes the order may be important.

Update:
The thing is that I lose the asvantage of my code because it is Pascal.
It would be nice if the comparison loop ( Path1[i]<> Path2[i] ) would be optimized to use Borland’s ASM code.


Delphi 7, Win XP 32 bit, Tests were done with 577 items in the list. Deleting the items from list IS NOT A PROBLEM because it happens rarely.


CONCLUSION

As Svein Bringsli pointed, my code is slow not because of the comparing algorithm but because of TListBox. The BEST solution was provided by Marcelo Cantos. Thanks a lot Marcelo.
I accepted Svein’s answer because it answers directly my question “how to make my comparison function faster” with “there is no point to make it faster”.
For the moment I implemented the dirty and quick to implement solution: when I have under 200 files, I use my slow code to check the duplicates. If there are more than 200 files I use dwrbudr’s solution (which is damn fast) considering that if the user has so many files, the order is irrelevant anyway (human brain cannot track so many items).

I want to thank you all for ideas and especially Svein for revealing the truth: (Borland’s) visual controls are damn slow!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:41:30+00:00Added an answer on May 16, 2026 at 6:41 pm

    Using your code as a starting point, I modified it to take a copy of the list before searching for duplicates. The time went from 5,5 seconds to about 0,5 seconds.

    vSL := TStringList.Create;
    try
      vSL.Assign(jList.Items);
      vSL.Sorted := true;
      for x:= vSL.Count-1 downto 1 DO
      begin
       sMaster:= vSL[x];
       for y:= x-1 downto 0 DO
        if SameFile(sMaster, vSL[y]) then
         begin
          vSL.Delete (x); { REMOVE DUPLICATES }
          jList.Items.Delete (x);
          Break;
         end;
      end;
    finally
      vSL.Free;
    end;
    

    Obviously, this is not a good way to do it, but it demonstrates that TFileListBox is in itself quite slow. I don’t believe you can gain much by optimizing your compare-function.

    To demonstrate this, I replaced your SameFile function with the following, but kept the rest of your code:

    function SameFile(CONST Path1, Path2: string): Boolean;
    VAR i: Integer;
    begin
      Result := false; //Pretty darn fast code!!!
    end;
    

    The time went from 5,6 seconds to 5,5 seconds. I don’t think there’s much more to gain there 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Check if all items are the same in a List I have
I have a function CheckMobile(MobNumber) and it will check all duplicate record of MobNumber
I have to check if many URLs have redirects. Right now, I'm using a
Possible Duplicate: How can I compare (directory) paths in C#? I have a filename
Possible Duplicate: Determining if an unordered vector<T> has all unique elements I have to
Possible Duplicate: Check status of services that run in a remote computer using C#
Possible Duplicate: Check of specific radio button is checked I have these 2 radio
Possible Duplicate: Check if value isset and null If I have $v = NULL;
Possible Duplicate: JQuery to check for duplicate ids in a DOM Suppose i have
Possible Duplicate: Check if JavaScript is enabled with PHP Is there some way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.