首页 > Windows开发 > 详细

C# the comparison between FileStream.Write() and StreamWriter.Write()

时间:2020-06-01 23:10:39      阅读:78      评论:0      收藏:0      [点我收藏+]
 
 static void FileStreamSDemo()
        {
            DateTime dtNow = DateTime.Now;
            DateTime dtEnd = dtNow.AddSeconds(60);
            using(FileStream fs=new FileStream("FileStream.txt",FileMode.Create))
            {
                while (DateTime.Now < dtEnd)
                {
                    string msg = $"Now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")},Guid is {Guid.NewGuid()}";
                    byte[] data = Encoding.UTF8.GetBytes(msg);
                    fs.Write(data, 0, data.Length);
                }
            }           
        }

        static void StreamWriterDemo()
        {
            DateTime dtNow = DateTime.Now;
            DateTime dtEnd = dtNow.AddSeconds(60);
            using(StreamWriter writer=new StreamWriter("StreamWriter.txt",true,Encoding.UTF8))
            {
                while(DateTime.Now<dtEnd)
                {
                    string msg = $"Now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")},Guid is {Guid.NewGuid()}";
                    writer.WriteLine(msg);
                }
            }
        }

 


 

In fact,during the identical time,the streamwriter generated much more data.

In my pc,StreamWriter.txt is 2.03 GB (2,187,534,336 bytes)

While FileStream.txt is 1.86 GB (1,998,835,712 bytes),

Based on the above fact and data,The streamwriter  has speed advantages.

C# the comparison between FileStream.Write() and StreamWriter.Write()

原文:https://www.cnblogs.com/Fred1987/p/13027736.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!