I tried unxutils' wc -l
but it crashed for 1GB files. I tried this C# code
long count = 0;
using (StreamReader r = new StreamReader(f))
{
string line;
while ((line = r.ReadLine()) != null)
{
count++;
}
}
return count;
It reads a 500MB file in 4 seconds
var size = 256;
var bytes = new byte[size];
var count = 0;
byte query = Convert.ToByte('
');
using (var stream = File.OpenRead(file))
{
int many;
do
{
many = stream.Read(bytes, 0, size);
count += bytes.Where(a => a == query).Count();
} while (many == size);
}
Reads in 10 seconds
var count = 0;
int query = (int)Convert.ToByte('
');
using (var stream = File.OpenRead(file))
{
int current;
do
{
current = stream.ReadByte();
if (current == query)
{
count++;
continue;
}
} while (current!= -1);
}
Takes 7 seconds
Is anything faster I haven't tried yet?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…