|
using System;
using System.Collections;
using System.Diagnostics;
namespace Common
{
internal class Profile
{
private DateTime _Start = DateTime.Now;
private ArrayList _Counters = new ArrayList();
private bool _IsRunning = false;
public bool IsRunning { get { return _IsRunning; } }
public Profile()
{
}
public void Start()
{
_IsRunning = true;
_Start = DateTime.Now;
Debug.WriteLine( "Profile started at " + _Start.ToString() );
}
public void Stop()
{
_IsRunning = false;
DateTime now = DateTime.Now;
Debug.WriteLine( "Profile finished at " + now.ToString() );
TimeSpan duration = now - _Start;
Debug.WriteLine( "Profile duration: " + duration.ToString() );
for ( int i = 0 ; i < _Counters.Count ; i++ )
{
ProfileCounter o = ( ProfileCounter ) _Counters[ i ];
Debug.WriteLine( "Counter " + o.Name + ":" );
Debug.WriteLine( " Called " + o.Calls + " times" );
Debug.WriteLine( " Total duration: " + o.Duration.ToString() );
double totalms = o.Duration.TotalMilliseconds;
double averagems = totalms / ( double ) o.Calls;
// TimeSpan a = TimeSpan.FromMilliseconds( averagems );
Debug.WriteLine( " Average duration: " + averagems + " ms" );
}
}
public ProfileCounter CreateCounter( string name )
{
ProfileCounter o = new ProfileCounter( name );
_Counters.Add( o );
return o;
}
}
internal class ProfileCounter
{
private string _Name = String.Empty;
private int _Calls = 0;
private TimeSpan _Duration = TimeSpan.Zero;
private DateTime _Start = DateTime.Now;
public string Name { get { return _Name; } }
public int Calls { get { return _Calls; } }
public TimeSpan Duration { get { return _Duration; } }
public ProfileCounter( string name )
{
_Name = name;
}
public void Call()
{
_Calls++;
_Start = DateTime.Now;
}
public void Return()
{
TimeSpan duration = DateTime.Now - _Start;
_Duration += duration;
}
}
internal class ProfileCall : IDisposable
{
private ProfileCounter _Counter = null;
public ProfileCall( ProfileCounter counter )
{
_Counter = counter;
_Counter.Call();
}
public void Dispose()
{
_Counter.Return();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since
In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.
I now live near idyllic Bournemouth in England.
I can work 'virtually' anywhere!