Click here to Skip to main content
15,883,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a arbitrary length list of sought-for file extensions.

When enumerating directories in search of files with those extensions (using Directory.EnumerateFiles, if it matters), which do you think would be faster?

A) Get a complete list of files, then ignore any that don't match the acceptable extensions?

or

B) Get multiple lists of files, each containing only those extensions that match?

I'm leaning toward B, if only because A could potentially create a huge list of files to filter through, whereas B comes pre-filtered.

But does going back into the same directories over and over to look for different file types add more overhead than just getting them all in one fell swoop?

--

The query used in enumerating the files is:
C#
var TheFiles = from file in
               Directory.EnumerateFiles(SearchPath, "*.*", SO)
               where file.Length > 0
               select file;


Would changing the "where" clause help? Something like

where file.Length > 0 AND (file.EndsWith(Ext1) OR (file.EndsWith(Ext2))

etc
Posted
Updated 22-Nov-10 5:04am
v2

1 solution

I have an idea. Since you're a programmer, why don't you try both ways and answer your own question?

EDIT --------------

I would use DirectoryInfo.GetFiles("*.xyz", true); and be done with it. BTW, LINQ is always slower.
 
Share this answer
 
v3
Comments
GenJerDan 22-Nov-10 11:10am    
Which I will, if no one already knows the answer off the top of their head. But why do it if someone else already did it and will share their experience?
If no one has, no problem. And I'll drop my results in here in case someone else ever needs to know.
#realJSOP 22-Nov-10 11:44am    
As a programming exercise, of course. Your question might get more attention if you specify the language and platform (C++, C#, WinForms, WPF, etc).
GenJerDan 23-Nov-10 11:56am    
Tried three different methods: enumerate all the files, then filter them; filter them while enumerating them; and using .GetFiles instead with the filtering coming afterward (LINQless).
The difference was a couple thousandths of a second on 4000+ files, and even that wasn't consistent in which method was "fastest". Lesson learned: it don't matter worth a darn which you use. :^)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900