## [TFindFile] Optimizing speed?

Please post bug reports, feature requests, or any question regarding the DELPHI AREA projects here.

### [TFindFile] Optimizing speed?

I am using TFindFile and it is faster if thread is used.
Do you have any idea to optimize the speed? maybe by create thread in FindFirstFile etc?
ina
Active Member

Posts: 14
Joined: November 24th, 2009, 4:34 pm

### Re: [TFindFile] Optimizing speed?

TFindFile is a bit faster when it runs in the main VCL thread (no separate thread).
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Optimizing speed?

In that code there is:
Specifies the maximum number of concurrent streams.0 - no restrictions

Honestly I dont know how to use that code but can you explain me the different about your thread and that thread? If you give me sample code to use that code is better The author's email is dead.
ina
Active Member

Posts: 14
Joined: November 24th, 2009, 4:34 pm

### Re: [TFindFile] Optimizing speed?

If you want to locate for a file in several different directories, my code uses one thread and searches the directories sequentially, but the other code searches the directories concurrently using a separate thread for each one.

Having more threads may sounds a faster search but in practice it may causes more overload because of context switching and code that is required to control the threads. If we could run each thread on a dedicated processor, then we could have a faster code.

In a system with a limited number of processors, you have to see threads as a tool to prevent blocking other part of the application, not gaining speed.

By the way, if we look for a file in a specific directory (including its sub-directories), both codes work similar.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Optimizing speed?

Kambiz wrote:TFindFile is a bit faster when it runs in the main VCL thread (no separate thread).

Can you tell me exactly what I am suppose to do? Steps to modify your code to make faster? I am just a beginner

Thx.
ina
Active Member

Posts: 14
Joined: November 24th, 2009, 4:34 pm

### Re: [TFindFile] Optimizing speed?

Just set Threaded property to False.

When I tell a bit faster it means about a milliseconds in searching thousands of files.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Optimizing speed?

Oh I see.. but that's bad for me
ina
Active Member

Posts: 14
Joined: November 24th, 2009, 4:34 pm

### Re: [TFindFile] Optimizing speed?

Kambiz, I want to ask again

It seems your TFindFile cannot scan multiple files at the same time, for example when code enter event FindFileFileMatch, it wait for that event finished then continue the scan. Is it right? If it right, can you give me some clues to make TFindFile scan multiple files at the same time.
ina
Active Member

Posts: 14
Joined: November 24th, 2009, 4:34 pm

### Re: [TFindFile] Optimizing speed?

Do you mean for example looking for C:\*.txt and C:\*.doc at the same time?

In this case, TFindFile performs a search for *.txt and then *.doc in each subfolder. As you can guess, that means two times increase in the search time. However, if you search for *.* and use filters, you can easily speed up the search.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Optimizing speed?

I mean if I looking for C:\*.exe, and in FindFileFileMatch there is some routines to check the exe (may take a long time), Does TFindFile will continue for searching file or it will wait until the routines finished?
ina
Active Member

Posts: 14
Joined: November 24th, 2009, 4:34 pm

### Re: [TFindFile] Optimizing speed?

Oh, I got it now. TFindFile waits for event handlers to return.

You should initiate your own thread when processing of the matched files take a long time.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Optimizing speed?

Can you give me the pseudocode or some clues?
ina
Active Member

Posts: 14
Joined: November 24th, 2009, 4:34 pm

### Re: [TFindFile] Optimizing speed?

Implement your own thread class to do whatever you need on a matched file. Then create an instance of that thread when FindFile finds a file.

Code: Select all
procedure TForm1.FindFile1FileMatch(Sender: TObject;  const FileInfo: TFileDetails);begin  TMyThread.Create(FileInfo);end;
Kambiz

Kambiz