I have a File Server which is mapped to a network drive letter W. it has 732 main folders and sub folders and a lot of files. I have mapped a virtual Directory to this drive and named it “Documents”.
Now I want to check how much time it takes to find only the folders which contains “final” in it. i created a Console app not a web app yet and write below code which gave me results very very slow which is not desired. Please find below the code
DateTime startdatetime = DateTime.Now;
var dirs = from dir in
Directory.EnumerateDirectories("W:\\", "*Final*", SearchOption.AllDirectories)
select dir;
foreach (var dir in dirs)
{
Console.WriteLine(dir);
}
DateTime EndDate = DateTime.Now;
TimeSpan t = EndDate - startdatetime;
Console.WriteLine(t.Minutes);
Console.ReadLine();
This gave me only folders not file names in 5 minutes on first run and 6 minutes on second.
Old code which i used to get only folders was
DateTime startdatetime = DateTime.Now;
string[] dirs =
Directory.GetDirectories("W:\\", "*Final*", SearchOption.AllDirectories);
foreach (var dir in dirs)
{
Console.WriteLine(dir);
}
DateTime EndDate = DateTime.Now;
TimeSpan t = EndDate - startdatetime;
Console.WriteLine(t.Minutes);
Console.ReadLine();
The above code gave me list of folders in 6-7 minutes.
Combined Files and Folders above code gave results in 12 minutes. That is huge performance penalty on web.
Any idea guys how to reduce this penalty cap? I am stuck here.
I feel iterating entire directory and files is very costly. Try to find out why you need that. Why not to get one directory and search files inside it. I mean try to do some analysis with your tech team and business owners so that every one is realistic.
The only thing that I can think of is using Parallel for each loop rather than the normal one so that you can utilize your CPU’s. But it will reduce the cost but not drastically.