C# Human Readable File Size Optimized Function
This is a highly optimized function to get the file size abbreviation in bytes for an arbitrary, 64-bit file size. It returns the file size to three decimals followed by the abbreviation, like KB, MB, GB, etc. Use this function to display a human-readable, user-friendly file size to your users.
Below is the human readable file size function source code. You can get a Visual Basic (VB.Net) version by using the C# to VB.Net Convertor Tool.
C# Human-Readable File Size Function
The code below is dedicated to the public domain under the Unlicense or CC0, whichever is most appropriate for your use. Attribution is not necessary, but a vote on the post at stackoverflow would be appreciated.
// Returns the human-readable file size for an arbitrary, 64-bit file size // The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB" public string GetBytesReadable(long i) { // Get absolute value long absolute_i = (i < 0 ? -i : i); // Determine the suffix and readable value string suffix; double readable; if (absolute_i >= 0x1000000000000000) // Exabyte { suffix = "EB"; readable = (i >> 50); } else if (absolute_i >= 0x4000000000000) // Petabyte { suffix = "PB"; readable = (i >> 40); } else if (absolute_i >= 0x10000000000) // Terabyte { suffix = "TB"; readable = (i >> 30); } else if (absolute_i >= 0x40000000) // Gigabyte { suffix = "GB"; readable = (i >> 20); } else if (absolute_i >= 0x100000) // Megabyte { suffix = "MB"; readable = (i >> 10); } else if (absolute_i >= 0x400) // Kilobyte { suffix = "KB"; readable = i; } else { return i.ToString("0 B"); // Byte } // Divide by 1024 to get fractional value readable = (readable / 1024); // Return formatted number with suffix return readable.ToString("0.### ") + suffix; }
Sample Usage
It is recommended to put the above function in a helper or utility class as a public static method.
// EXAMPLE OUTPUT GetSizeReadable(1023); // 1023 B GetSizeReadable(1024); // 1 KB GetSizeReadable(1025); // 1.001 KB // Example of getting a file size and converting it to a readable value string fileName = "abc.txt"; long fileSize = new System.IO.FileInfo(fileName).Length; string sizeReadable = GetSizeReadable(fileSize);
Discussion
I wrote this function after being sorely disappointed by the versions posted on StackOverflow, at How do I get a human-readable file size abbreviation using .NET? Those versions had obvious performance issues.
This is not a function likely to be called very frequently in any individual program, so it may not be important to your project to shave some microseconds off this function call. However, I'm posting this here because if you add up the number of times it is called all over the world, there is a great potential savings of CPU-cycles if everyone were to use an optimized version.
This version is about two times faster and more efficient overall than David Thibault's version, the top-rated post on StackOverflow as of this writing. (If you take out the ToString, the most expensive function, the math portion is over 10 times faster.) Here are the key optimizations and features versus other versions posted there.
- tested and in-use in a production environment
- loop unrolling
- binary arithmetic instead of potentially expensive functions like Math.Log and Math.Pow
- correctly handles all possible sizes in the 64-bit signed integers (long) returned by FileInfo.Length
- accurate up to 3 decimal places
- no rounding
- ToString instead of String.Format (much faster)
I'm just an average C# programmer with some experience in C++ optimization. If you find other ways to optimize this function, please let me know!
Disclaimer: This content is provided as-is. The information may be incorrect.