Untitled
unknown
csharp
2 years ago
1.7 kB
16
Indexable
// See https://aka.ms/new-console-template for more information
using System.Diagnostics;
using System.Text;
AppDomain.MonitoringIsEnabled = true;
var sp = Stopwatch.StartNew();
var records = new List<Record>();
const Int32 BufferSize = 128;
using (var fileStream = File.OpenRead(@"D:\summary.txt"))
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize)) {
while (streamReader.ReadLine() is { } line)
{
records.Add(new Record(line));
}
}
var summary = from record in records
group record by record.Id
into g
select new
{
Id = g.Key,
Duration = TimeSpan.FromTicks(g.Sum(r => r.Duration.Ticks))
};
using (var output = File.CreateText(@"D:\result.txt"))
{
foreach (var entry in summary)
{
output.WriteLine($"{entry.Id:D10} {entry.Duration:c}");
}
}
Console.WriteLine($"Took: {sp.ElapsedMilliseconds:#,#} ms and allocated " +
$"{AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize / 1024:#,#} kb " +
$"with peak working set of {Process.GetCurrentProcess().PeakWorkingSet64 / 1024:#,#} kb");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
// data class
public class Record
{
public DateTime Start { get; }
public DateTime End { get; }
public long Id { get; }
public TimeSpan Duration => End - Start;
public Record(string line)
{
var split = line.Split();
Start = Parse(split[0]);
End = Parse(split[1]);
Id = long.Parse(split[2]);
}
private static DateTime Parse(string str)
{
return DateTime.Parse(str);
}
}
Editor is loading...
Leave a Comment