C#统计文本文件中的行数,用3中方法对一个300k 的文件进行统计的结果



统计文本文件中的行数,用3中方法对一个300k 的文件进行统计的结果:
第一次:
StreamReader.Read():共2761行,耗时:6.08
FileStream.Read():共2761行,耗时:11.23
StreamReader.ReadLine():共2761行,耗时:3.61
第二次:
StreamReader.Read():共2761行,耗时:8.87
FileStream.Read():共2761行,耗时:14.74
StreamReader.ReadLine():共2761行,耗时:4.14
第三次:
StreamReader.Read():共2761行,耗时:6.39
FileStream.Read():共2761行,耗时:14.1
StreamReader.ReadLine():共2761行,耗时:4.76


本以为 StreamReader.ReadLine() 方法统计会很慢,结果.....。如果有快速统计算法分享下

http://blog.csdn.net/xxj_jing/article/details/52063883


测试代码如下:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace TestGrass.Log
{
    [TestClass]
    public class TestTxtRead
    {
        public string _minFile = @"D:\temp\logs\***.txt";
        public string _maxFile = @"D:\temp\logs\***.log";
        /// <summary>
        /// 回车符 \n=13=0x0D
        /// </summary>
        public byte _enter = 0x0D;
        /// <summary>
        /// 换行符 \r=10=0x0A
        /// </summary>
        public byte _return = 0x0A;
        [TestMethod]
        public void TestStream()
        {
            /*
             * 读取300k文件耗时
             * StreamReader.Read() 用时 8.19s
             * FileStream.Read() 用时
             * StreamReader.ReadLine() 用时 2.83s
             */
            byte n = 0xD;
            byte r = 0xA;
            StringBuilder msg = new StringBuilder();
            Stopwatch sw = new Stopwatch();
            var path = _minFile;
            int lines1 = 0;
            int lines2 = 0;
            int lines3 = 0;
            //单个字符读取
            sw.Start();
            using (var sr = new StreamReader(path))
            {
                int val = 0;
                val = sr.Read();
                //while ((val=sr.Read()) != -1)
                while(val!=-1)
                {
                    if (val == n)
                        lines1++;
                    val = sr.Read();
                }
            }
            sw.Stop();
            msg.AppendLine(string.Format("StreamReader.Read():共{0}行,耗时:{1}"
                ,lines1
                ,Math.Round(sw.ElapsedTicks*1.0/1000,2)));


            //使用缓冲读取
            Action<byte[]> totalizer = (arr)=>
            {
                lines2 += arr.Count(x => { return x == n; });
            };
            sw.Restart();
            
            using (var fs = new FileStream(path,FileMode.Open))
            {
                var buffer = new byte[1024];
                var rc = fs.Read(buffer, 0, buffer.Length);
                totalizer(buffer);
                
                while (rc!=0)
                {
                    buffer = new byte[1024];
                    rc = fs.Read(buffer, 0, buffer.Length);
                    totalizer(buffer);
                }
            }
            sw.Stop();
            msg.AppendLine(string.Format("FileStream.Read():共{0}行,耗时:{1}"
                , lines2
                , Math.Round(sw.ElapsedTicks * 1.0 / 1000, 2)));

            //按行读取
            sw.Restart();
            using (var sr = new StreamReader(path))
            {
                var ls = "";
                while ((ls=sr.ReadLine()) != null)
                {
                    lines3++;
                }
            }
            sw.Stop();
            msg.AppendLine(string.Format("StreamReader.ReadLine():共{0}行,耗时:{1}"
                , lines3
                , Math.Round(sw.ElapsedTicks * 1.0 / 1000, 2)));
            string str = msg.ToString();
            Assert.IsTrue(true);
        }
    }

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值