Skip to content

Instantly share code, notes, and snippets.

@ayende ayende/Analyze-File.cs
Last active Nov 16, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.