C# – Performance issues in FileStream.Write while writing bytes decoded using AsciiEncoding.GetBytes and Convert.FromBase64String

cdecodingencodingfilestreamnet

I am facing a performance problem while using the FileStream.Write function.

I have a console application that i use to read a Base64 string from a file (~ size is 400 KB) using a StreamReader object. I convert this string to a byte array using Convert.FromBase64String. I then write this byte array to a file using the FileStream object. The byte array length obtained here was 334991.

I measured the time taken to write the byte array – and it comes out to be approximately 0.116 seconds.

Just for fun, i got the array of bytes from the same Base64 encoded string using the ASCIIEncoding.GetBytes function (even though i knew that that this would not give the correct DECODED output – i just wanted to try it out). I wrote this byte array to the file using a FileStream object. The byte array length obtained here was 458414.

I measured the time taken to write the byte array using this methodology – and it comes out to be approximately 0.008 seconds.

Here is the sample code:

class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();
        TimeSpan executionTime;

        StreamReader sr = new StreamReader("foo.txt");
        string sampleString = sr.ReadToEnd();
        sr.Close();

        ////1. Convert to bytes using Base64 Decoder (The real output!)
        //byte[] binaryData = Convert.FromBase64String(sampleString);

        //2. Convert to bytes using AsciiEncoding (Just for Fun!)
        byte[] binaryData = new System.Text.ASCIIEncoding().GetBytes(sampleString);
        Console.WriteLine("Byte Length: " + binaryData.Length);

        stopWatch.Start();
        FileStream fs = new FileStream("bar.txt", FileMode.Create, FileAccess.Write);
        fs.Write(binaryData, 0, binaryData.Length);
        fs.Flush();
        fs.Close();
        stopWatch.Stop();

        executionTime = stopWatch.Elapsed;
        Console.WriteLine("FileStream Write - Total Execution Time: " + executionTime.TotalSeconds.ToString());
        Console.Read();
    }
}

I ran tests for approximately 5000 files containing Base64 encoded string and the difference between the time taken to write these two types of byte array is almost a factor of 10 (with the one with writing the byte array using the real decoding taking more time).

The length of the byte array obtained using Convert.FromBase64String is less than the one obtained using an ASCIIEncoding.GetBytes function.

I wonder that all that I am trying to do is write a bunch of bytes using a FileStream object. So why should there be such a drastic performance difference (w.r.t. time taken) while writing a byte array to the disk?

Or am i doing something terribly wrong? Please advise.

Best Answer

For starters, DateTime has a low resolution (iirc 0.018 s). So better use a stopwatch class.

Now this doesn't fully explain the difference, but you might get some better numbers.

Related Topic