統計文本檔案中的行數,用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);
}
}
}