Skip to content

Instantly share code, notes, and snippets.

@disouzam
Forked from ayende/Analyze-File.cs
Created August 28, 2024 12:35
Show Gist options
  • Save disouzam/2f317618d5fdcdae4b8c640a27aa423a to your computer and use it in GitHub Desktop.
Save disouzam/2f317618d5fdcdae4b8c640a27aa423a to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace FileAnalyzer
{
public class Record
{
public DateTime Start => DateTime.Parse(_line.Split(' ')[0]);
public DateTime End => DateTime.Parse(_line.Split(' ')[1]);
public long Id => long.Parse(_line.Split(' ')[2]);
public TimeSpan Duration => End - Start;
private readonly string _line;
public Record(string line)
{
_line = line;
}
}
class Program
{
static void Main(string[] args)
{
AppDomain.MonitoringIsEnabled = true;
var sp = Stopwatch.StartNew();
var summary = from line in File.ReadAllLines(args[0])
let record = new Record(line)
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("summary.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");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment