3

I am writing a method to read large files using new ReadAsync methods. In my testing looks like FileStream ReadAsync is faster than StreamReader, not sure why?

ReadStreamReaderAsync

Thread ID Before Await : 9

Thread ID After Await : 13

Time : 76626

Total Bytes : 687184052

ReadFileStreamAsync

Thread ID Before Await : 9

Thread ID After Await : 10

Time : 19167

Total Bytes : 687184052

  static async Task<long> ReadStreamReaderAsync(string filename)
    {
        Console.WriteLine("Thread ID Before Await : {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
        long totalBytes = 0;
        var sp = new Stopwatch();
        sp.Start();
        using (StreamReader reader = new StreamReader(filename, Encoding.Default))
        {
            char[] buffer = new char[0x1000];
            int numRead;
            while ((numRead = await reader.ReadAsync(buffer, 0, buffer.Length)) != 0)
            {
                totalBytes += numRead;
            }
        }

        sp.Stop();
        Console.WriteLine("Thread ID After Await : {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
        Console.WriteLine("Time : {0}", sp.ElapsedMilliseconds);
        return totalBytes;
    }

    static async Task<long> ReadFileStreamAsync(string filePath)
    {
        Console.WriteLine("Thread ID Before Await : {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
        long totalBytes = 0;
        var sp = new Stopwatch();
        sp.Start();
        using (FileStream sourceStream = new FileStream(filePath,
            FileMode.Open, FileAccess.Read, FileShare.Read,
            bufferSize: 4096, useAsync: true))
        {
            byte[] buffer = new byte[0x1000];
            int numRead;
            while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
            {
                totalBytes += numRead;
            }          
        }

        sp.Stop();
        Console.WriteLine("Thread ID After Await : {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
        Console.WriteLine("Time : {0}", sp.ElapsedMilliseconds);
        return totalBytes;
    }
1
  • 1
    What do they do different? Do you think that could account for it? Commented Apr 1, 2014 at 15:50

1 Answer 1

6

StreamReader is using an encoder to read the data (UTF-8).

If you have used a UTF-8 based file you will potentially have received less data as UTF-8 can encoded to many bytes and the Encoder will understand this.

FileStream is dumb and is giving you raw data and trusting you know how to handle it. So if for instance you are reading a text file you should use StreamReader (with the correct encoder)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.