您的位置:首页 > 编程语言 > C#

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
测试代码如下:

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);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: