none
MemoryStream vs FileStream RRS feed

Answers

  • Without context, it is impossible to properly answer this question.

    However, since a memory stream reads from memory and a  file stream reads from disk (unless the file contents have been cached by the operating system) then in general a memory stream will be hundreds or even thousands of times faster.

    This, of course, ignores the question of how the data got into the memory stream in the first place.
    Friday, November 30, 2007 10:16 AM
  • I'd agree with Matthew.  MemoryStream performs i/o to memory, FileStream to a file.  They're not really interchangable.  From a performance standpoint, read/writing to memory is always going to be faster than read/writing to disk.

     

    Myabe if you provide some more detail as to why you need to compare MemoryStream to FileStream.

     

    Friday, November 30, 2007 2:06 PM
    Moderator

All replies

  • Hi,

     

    Stream is a representation of bytes. Both these classes derive from the Stream class which is abstract by definition.

    As the name suggests, a FileStream reads and writes to a file whereas a MemoryStream reads and writes to the memory. So it relates to where the stream is stored.

     

    Now it depends how you plan to use both of these. For eg: Let us assume you want to read binary data from the database, you would go in for a MemoryStream. However if you want to read a file on your system, you would go in for a FileStream.

     

    One quick advantage of a MemoryStream is that there is not need to create temporary buffers and files in an application.

     

     

    HTH,
    Suprotim Agarwal

    -----
    http://www.dotnetcurry.com
    -----

     

     

     

    Friday, November 30, 2007 5:02 AM
    Moderator
  •  

    I would presume that MemoryStream is quicker, hence it probably isn't Smile

     

    But they are both used in a very similiar fashion.  If your looking at a 400 Megs worth of data, perhaps don't use a memory stream due to ram limitations Smile. An app I was working on once used a memory stream bust consumed 3X the amout of ram that you might expect it to Smile. There was a technical reason for it to.

    Friday, November 30, 2007 10:04 AM
  • Without context, it is impossible to properly answer this question.

    However, since a memory stream reads from memory and a  file stream reads from disk (unless the file contents have been cached by the operating system) then in general a memory stream will be hundreds or even thousands of times faster.

    This, of course, ignores the question of how the data got into the memory stream in the first place.
    Friday, November 30, 2007 10:16 AM
  • I'd agree with Matthew.  MemoryStream performs i/o to memory, FileStream to a file.  They're not really interchangable.  From a performance standpoint, read/writing to memory is always going to be faster than read/writing to disk.

     

    Myabe if you provide some more detail as to why you need to compare MemoryStream to FileStream.

     

    Friday, November 30, 2007 2:06 PM
    Moderator
  •  

    thanks for the reply man! thats what i want to know which one is faster... Smile
    Thursday, December 20, 2007 9:25 AM
  • I have a scenario where i need to decide on whether to use memory stream or file stream.

    I have mentioned two approaches below and think that Approach1 is better than Approach2 in this scenario, as the RAM usage will be huge if i use a memory stream for all the files.

    Which one do you suggest?

    Scenario:
    1) There are "n" number of files in a folder, where n > 100
    2) All these files data needs to merged/appended in to a single destination file.

    Approach1:
    For each file, append the data directly in to the destination file using a filestream.
    Code Snippet Sample:
    using (FileStream fsDestDocFile = File.Open(destDir + destDocFileName, FileMode.Append, FileAccess.Write))
    {
          // For each source File

          Byte[] srcFileByte = File.ReadAllBytes(sourceFile);
          fsDestDocFile.Write(srcFileByte , 0, srcFileByte.Length);
    }

    Approach2:
    1) For each file, append the data in a memory stream.
    2) Finally, write the memory stream to the destination file.

     

    Wednesday, September 1, 2010 6:00 PM
  • The total time used to write the data to the disk won't be different. Write it directly. You'll never notice it's writing if it takes 1 ms 1000 times. You'll notice it if it takes 1 s once.
    Wednesday, September 1, 2010 10:46 PM