Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to read file length C#

I am using fs.Length, where fs is a FileStream.

Is this an O(1) operation? I would think this would just read from the properties of the file, as opposed to going through the file to find when the seek position has reached the end. The file I am trying to find the length of could easily range from 1 MB to 4-5 GB.

However I noticed that there is a FileInfo class, which also has a Length property.

Do both of these Length properties theoretically take the same amount of time? Or does is fs.Length slower because it must open the FileStream first?

like image 737
jpints14 Avatar asked Feb 07 '12 15:02

jpints14


People also ask

How do I get the size of a file in C?

Using stat() function The stat() function takes the file path and returns a structure containing information about the file pointed by it. To get the size of the file in bytes, use the st_size field of the returned structure.

How do I read file size?

Locate the file or folder whose size you would like to view. Click the file or folder. Press Command + I on your keyboard. A window opens and shows the size of the file or folder.

What is file * ft in C?

FILE *fp; To open a file you need to use the fopen function, which returns a FILE pointer. Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file.

What is a size T?

The datatype size_t is unsigned integral type. It represents the size of any object in bytes and returned by sizeof operator. It is used for array indexing and counting. It can never be negative. The return type of strcspn, strlen functions is size_t.


2 Answers

The natural way to get the file size in .NET is the FileInfo.Length property you mentioned.

I am not sure Stream.Length is slower (it won't read the whole file anyway), but it's definitely more natural to use FileInfo instead of a FileStream if you do not plan to read the file.


Here's a small benchmark that will provide some numeric values:

private static void Main(string[] args) {     string filePath = ...;   // Path to 2.5 GB file here      Stopwatch z1 = new Stopwatch();     Stopwatch z2 = new Stopwatch();      int count = 10000;      z1.Start();     for (int i = 0; i < count; i++)     {         long length;         using (Stream stream = new FileStream(filePath, FileMode.Open))         {             length = stream.Length;         }     }      z1.Stop();      z2.Start();     for (int i = 0; i < count; i++)     {         long length = new FileInfo(filePath).Length;     }      z2.Stop();      Console.WriteLine(string.Format("Stream: {0}", z1.ElapsedMilliseconds));     Console.WriteLine(string.Format("FileInfo: {0}", z2.ElapsedMilliseconds));      Console.ReadKey(); } 

Results:

Stream: 886 FileInfo: 727 
like image 70
ken2k Avatar answered Sep 28 '22 19:09

ken2k


Both will access the file system metadata rather than reading the whole file. I don't know which is more efficient necessarily, as a rule of thumb I'd say that if you only want to know the length (and other metadata), use FileInfo - whereas if you're opening the file as a stream anyway, use FileStream.Length.

like image 27
Jon Skeet Avatar answered Sep 28 '22 19:09

Jon Skeet