正在处理一个将加载 100+ GB 文本文件的项目,其中一个过程是计算指定文件中的行数。我必须按照以下方式执行此操作,以免出现内存异常。有没有更快的方法或者最有效的方法来处理多任务。 (我知道你可以做一些事情,比如在 4 个线程上运行它,然后将组合输出除以 4。不知道最有效的方法)
uint loadCount2 = 0;
foreach (var line in File.ReadLines(currentPath))
{
loadCount2++;
}
当我修复了程序的位置后,计划在具有 4 个双核 CPU 和 40 GB RAM 的服务器上运行该程序。目前,它运行在临时的小型 4 核 8GB RAM 服务器上。 (不知道线程在多个 CPU 上的表现如何。)
我测试了很多你的建议。
Stopwatch sw2 = Stopwatch.StartNew();
{
using (FileStream fs = File.Open(json, FileMode.Open))
CountLinesMaybe(fs);
}
TimeSpan t = TimeSpan.FromMilliseconds(sw2.ElapsedMilliseconds);
string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
Console.WriteLine(answer);
sw2.Restart();
loadCount2 = 0;
Parallel.ForEach(File.ReadLines(json), (line) =>
{
loadCount2++;
});
t = TimeSpan.FromMilliseconds(sw2.ElapsedMilliseconds);
answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
Console.WriteLine(answer);
sw2.Restart();
loadCount2 = 0;
foreach (var line in File.ReadLines(json))
{
loadCount2++;
}
t = TimeSpan.FromMilliseconds(sw2.ElapsedMilliseconds);
answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
Console.WriteLine(answer);
sw2.Restart();
loadCount2 = 0;
int query = (int)Convert.ToByte('\n');
using (var stream = File.OpenRead(json))
{
int current;
do
{
current = stream.ReadByte();
if (current == query)
{
loadCount2++;
continue;
}
} while (current != -1);
}
t = TimeSpan.FromMilliseconds(sw2.ElapsedMilliseconds);
answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
Console.WriteLine(answer);
Console.ReadKey();
private const char CR = '\r';
private const char LF = '\n';
private const char NULL = (char)0;
public static long CountLinesMaybe(Stream stream)
{
//Ensure.NotNull(stream, nameof(stream));
var lineCount = 0L;
var byteBuffer = new byte[1024 * 1024];
const int BytesAtTheTime = 4;
var detectedEOL = NULL;
var currentChar = NULL;
int bytesRead;
while ((bytesRead = stream.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
var i = 0;
for (; i <= bytesRead - BytesAtTheTime; i += BytesAtTheTime)
{
currentChar = (char)byteBuffer[i];
if (detectedEOL != NULL)
{
if (currentChar == detectedEOL) { lineCount++; }
currentChar = (char)byteBuffer[i + 1];
if (currentChar == detectedEOL) { lineCount++; }
currentChar = (char)byteBuffer[i + 2];
if (currentChar == detectedEOL) { lineCount++; }
currentChar = (char)byteBuffer[i + 3];
if (currentChar == detectedEOL) { lineCount++; }
}
else
{
if (currentChar == LF || currentChar == CR)
{
detectedEOL = currentChar;
lineCount++;
}
i -= BytesAtTheTime - 1;
}
}
for (; i < bytesRead; i++)
{
currentChar = (char)byteBuffer[i];
if (detectedEOL != NULL)
{
if (currentChar == detectedEOL) { lineCount++; }
}
else
{
if (currentChar == LF || currentChar == CR)
{
detectedEOL = currentChar;
lineCount++;
}
}
}
}
if (currentChar != LF && currentChar != CR && currentChar != NULL)
{
lineCount++;
}
return lineCount;
}
结果显示出很大的进步,但我希望达到 20 分钟。 我希望他们在我更强大的服务器上这样做,看看拥有更多 CPU 的效果。
第二次运行返回: 23分钟, 25分钟, 22分钟, 29 分钟
这意味着这些方法实际上没有任何区别。 (无法截图,因为我取消了暂停,程序通过清屏继续)
最佳答案
基于 ReadByte(并与换行符比较)的方法可能比 ReadLine 更快。例如,对于更接近 GB 的文件
stopwatch = System.Diagnostics.Stopwatch.StartNew();
uint count = 0;
int query = (int)Convert.ToByte('\n');
using (var stream = File.OpenRead(filepath))
{
int current;
do
{
current = stream.ReadByte();
if (current == query)
{
count++;
continue;
}
} while (current!= -1);
}
Console.WriteLine($"Using ReadByte,Time : {stopwatch.Elapsed.TotalMilliseconds},Count: {r}");
使用ReadByte,时间:8174.5661,计数:7555107
stopwatch = System.Diagnostics.Stopwatch.StartNew();
uint loadCount2 = 0;
foreach (var line in File.ReadLines(filepath))
{
loadCount2++;
}
Console.WriteLine($"Using ReadLines, Time : {stopwatch.Elapsed.TotalMilliseconds},Count: {r}");
使用 ReadLines,时间:27303.835,计数:7555107
关于c# - 在 C# 中快速读取 100+ GB 文件中的行的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53576797/