C#统计文本文件中的行数,用3中方法对一个300k 的文件进行统计的结果
2016-07-29 13:22
411 查看
统计文本文件中的行数,用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
测试代码如下:
第一次:
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); } } }
相关文章推荐
- 对shell命令结果进行行数统计的方法
- linux shell对行数进行统计的方法
- C++文件读写操作(三)如何统计文本的行数及如何读取文件某一行内容
- Shell脚本统计文件行数的8种方法
- Shell脚本统计文件行数的8种方法
- php 统计文件行数方法
- 使用shell 统计文本行数,输出两个文件不同或相同的行
- Python3读取UTF-8文件及统计文件行数的方法
- 单线程解析文件,根据文件关键词进行统计,并将结果输出(一)
- python计算文本文件行数的方法
- 文件读取C++文件读写操作(三)如何统计文本的行数及如何读取文件某一行内容
- Shell脚本统计文件行数的8种方法
- 刚想到一个统计文件行数的方法
- 单线程解析文件,根据文件关键词进行统计,并将结果输出(二)
- 统计一个大文本行数的几种方法以及效率统计(一)
- 小技巧:统计文本文件行数
- Shell脚本统计文件行数的8种方法
- Shell脚本统计文件行数的8种方法
- linux下wc统计文件的个数、行数、字数、字节数等信息方法