Iterating Over a DirectoryInfo is Fast!

DirectoryInfo is really very fast to iterate through. While working on some software, I’ve been experimenting with different approaches to find the most efficient and fastest ways to get things done. Here are two different methods that achieve the same result, but in different ways and with radically different speeds:

 private static long[] AllSizesInDirectory(DirectoryInfo di)
 {
     Stopwatch sw = new Stopwatch();
     sw.Start();
     long[] result = null;
     if (di == null)
	 return result;

     List<long> sizes = new List<long>();

     foreach (FileInfo fi in di.GetFiles())
     {
	 sizes.Add(fi.Length);
     }

     result = sizes.ToArray();
     sw.Stop();
     Console.WriteLine("ELAPSED 1: " + sw.ElapsedTicks.ToString());
     return result;
 }

The next method takes a string[] of file names:

 private static long[] AllSizesInDirectory(string[] folder)
 {
     Stopwatch sw = new Stopwatch();
     sw.Start();
     long[] result = null;
     if (folder == null || folder.Length == 0)
	 return result;

     List<long> sizes = new List<long>();

     FileInfo fi;

     for (int i = 0; i < folder.Length; i++)
     {
	 fi = new FileInfo(folder[i]);
	 sizes.Add(fi.Length);
     }
     result = sizes.ToArray();
     sw.Stop();
     Console.WriteLine("ELAPSED 2: " + sw.ElapsedTicks.ToString());
     return result;
 }

Running the following test:

for (int i = 0; i < 10; i++)
{
     long[] l = AllJpgSizesInDirectory(di1);
     long[] ll = AllJpgSizesInDirectory(folder1);
}

I get the following result:

ELAPSED 1: 28144
ELAPSED 2: 1932801

ELAPSED 1: 35950
ELAPSED 2: 157037

ELAPSED 1: 27622
ELAPSED 2: 168033

ELAPSED 1: 25636
ELAPSED 2: 143944

ELAPSED 1: 25146
ELAPSED 2: 145443

ELAPSED 1: 34667
ELAPSED 2: 151924

ELAPSED 1: 26099
ELAPSED 2: 149847

ELAPSED 1: 33042
ELAPSED 2: 143732

ELAPSED 1: 24985
ELAPSED 2: 142945

ELAPSED 1: 24801
ELAPSED 2: 142366

The first method comes in around 25,000 ticks, while the second clocks in around 150,000 ticks, or about 6x slower for a directory of 215 JPGs of 1.19 GB. The difference gets larger with larger directories.

Anyways, it’s a stark example of just how efficient enumerating over a collection can be as opposed to doing things manually.

Share

Written by:

279 Posts

View All Posts
Follow Me :

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.