Skip to content

Instantly share code, notes, and snippets.

@lynxz lynxz/AnalyzeFile.cs
Created Nov 13, 2016

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace AnalyzeFile {
class Program {
const int IdSize = 8;
const int DateSize = 19;
const int SecondDateOffset = DateSize + 1;
const int IdOffset = SecondDateOffset + DateSize + 1;
const int RowSize = 50;
const int OutputRowSize = 24;
static void Main( string[ ] args ) {
AppDomain.MonitoringIsEnabled = true;
var sp = Stopwatch.StartNew( );
var summary = new Dictionary<long, TimeSpan>( );
using( var fs = new FileStream( args[ 0 ], FileMode.Open, FileAccess.Read ) ) {
var buff = new byte[ RowSize ];
while( fs.Read( buff, 0, RowSize ) > 0 ) {
var start = GetEntryDate( buff );
var stop = GetLeaveDate( buff );
var id = GetIdValue( buff );
var duration = stop - start;
if( !summary.ContainsKey( id ) ) {
summary.Add( id, TimeSpan.Zero );
}
summary[ id ] += duration;
}
}
using( var output = new FileStream( "summary.txt", FileMode.Create, FileAccess.Write ) ) {
var buff = new byte[ OutputRowSize ];
foreach( var entry in summary ) {
EnterId( buff, entry.Key );
EnterTimeSpan( buff, entry.Value );
EnterNewLine( buff );
output.Write( buff, 0, OutputRowSize );
}
}
Console.WriteLine( $"Took: {sp.ElapsedMilliseconds:#,#} ms and allocated " +
$"{AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize / 1024:#,#} kb " +
$"with peak working set of {Process.GetCurrentProcess( ).PeakWorkingSet64 / 1024:#,#} kb" );
}
static void EnterNewLine( byte[ ] array ) {
array[ 22 ] = 13;
array[ 23 ] = 10;
}
static void EnterTimeSpan( byte[ ] array, TimeSpan duration ) {
array[ 10 ] = 32;
EnterDigits( array, 11, duration.Days );
array[ 13 ] = ( byte )'.';
EnterDigits( array, 14, duration.Hours );
array[ 16 ] = ( byte )':';
EnterDigits( array, 17, duration.Minutes );
array[ 19 ] = ( byte )':';
EnterDigits( array, 20, duration.Seconds );
}
static void EnterDigits( byte[ ] array, int offset, int value ) {
array[ offset ] = ( byte )( '0' + value / 10 );
array[ offset + 1 ] = ( byte )( '0' + value % 10 );
}
static void EnterId( byte[ ] array, long id ) {
var divider = 1000000000L;
for( int i = 0; i < 10; i++ ) {
array[ i ] = ( byte )( '0' + ( byte )( id / divider ) );
id %= divider;
divider /= 10;
}
}
static long GetIdValue( byte[ ] array ) {
var result = 0L;
for( int i = 0; i < IdSize; i++ ) {
result *= 10;
result += array[ i + IdOffset ] - '0';
}
return result;
}
static DateTime GetEntryDate( byte[ ] array ) {
return new DateTime(
GetYear( array, true ),
GetMonth( array, true ),
GetDate( array, true ),
GetHours( array, true ),
GetMinutes( array, true ),
GetSeconds( array, true ) );
}
static DateTime GetLeaveDate( byte[ ] array ) {
return new DateTime(
GetYear( array, false ),
GetMonth( array, false ),
GetDate( array, false ),
GetHours( array, false ),
GetMinutes( array, false ),
GetSeconds( array, false ) );
}
static int GetSeconds( byte[ ] array, bool start ) {
return start ? GetValue( array, 17, 2 ) : GetValue( array, SecondDateOffset + 17, 2 );
}
static int GetMinutes( byte[ ] array, bool start ) {
return start ? GetValue( array, 14, 2 ) : GetValue( array, SecondDateOffset + 14, 2 );
}
static int GetHours( byte[ ] array, bool start ) {
return start ? GetValue( array, 11, 2 ) : GetValue( array, SecondDateOffset + 11, 2 );
}
static int GetDate( byte[ ] array, bool start ) {
return start ? GetValue( array, 8, 2 ) : GetValue( array, SecondDateOffset + 8, 2 );
}
static int GetMonth( byte[ ] array, bool start ) {
return start ? GetValue( array, 5, 2 ) : GetValue( array, SecondDateOffset + 5, 2 );
}
static int GetYear( byte[ ] array, bool start ) {
return start ? GetValue( array, 0, 4 ) : GetValue( array, SecondDateOffset, 4 );
}
static int GetValue( byte[ ] array, int offset, int size ) {
var result = 0;
for( int i = 0; i < size; i++ ) {
result *= 10;
result += array[ i + offset ] - '0';
}
return result;
}
}
}
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.