如何快速计算行数?
|
我尝试了unxutils \'0',但是它崩溃了1GB文件。我尝试了此C#代码
long count = 0;
using (StreamReader r = new StreamReader(f))
{
string line;
while ((line = r.ReadLine()) != null)
{
count++;
}
}
return count;
它在4秒钟内读取500MB文件
var size = 256;
var bytes = new byte[size];
var count = 0;
byte query = Convert.ToByte(\'\\n\');
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);
}
10秒内读取
var count = 0;
int query = (int)Convert.ToByte(\'\\n\');
using (var stream = File.OpenRead(file))
{
int current;
do
{
current = stream.ReadByte();
if (current == query)
{
count++;
continue;
}
} while (current!= -1);
}
需要7秒
我还没有尝试过更快的速度吗?
没有找到相关结果
已邀请:
6 个回复
晤默报
瞧叮
与第一个代码段相同的时间为4秒
摊揉售
嘘伪
盟犯涩沟都
味芯憨
只需编译:
它接受stdin上的输入并输出行数。