统计文本文件中的行数,用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);
}
}
}
今天的文章统计表格内指定文本的个数_程序员一天写多少行代码「建议收藏」分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/62382.html