>using GregoryAdam.Base.ExtensionMethods; > > class test > { > static void Main(string[] args) > { > ((delegateVoid)TestLoop).ExecuteTimed(true); > } > > > static void TestLoop() > { > for (int i = int.MaxValue/2; --i != 0; ) > ; > } > } >>
>using System; >using System.Collections.Generic; >using System.Linq; >using System.Text; > >// delegates >namespace GregoryAdam.Base.ExtensionMethods >{ > public delegate void delegateVoid(); > > public static partial class ExtensionMethods > { > > #region ExecuteTimed > > > private static Stack<DateTime> Times = new Stack<DateTime>(); > private static Stack<string> Subject = new Stack<string>(); > //------------------------------------------------------------------------- > public static void ExecuteTimed(this delegateVoid function) > { > ExecuteTimed(function, false); > } > > public static void ExecuteTimed(this delegateVoid function, bool pause) > { > Start(function.GetInvocationList()[0].Method.Name + "()"); > function(); > End(pause); > } > > //------------------------------------------------------------------------- > private static void Start(string subject) > { > lock (Times) > { > Subject.Push(subject); > Times.Push(DateTime.Now); > } > } > //------------------------------------------------------------------------- > private static void End() > { > End(false); > } > //------------------------------------------------------------------------- > private static void End(bool pause) > { > TimeSpan elapsed; > string subject; > > lock (Times) > { > if (Times.Count == 0) > { > System.Diagnostics.Debug.Assert(false, "Time Stack empty"); > return; > } > > elapsed = DateTime.Now - Times.Pop(); > subject = Subject.Pop(); > } > > Console.WriteLine("{0} : {1:00}:{2:00}:{3:00}.{4:00}", > subject, > elapsed.Hours, > elapsed.Minutes, > elapsed.Seconds, > elapsed.Milliseconds / 10 > ); > > if (pause) > { > Console.WriteLine("Enter to continue"); > Console.ReadLine(); > } > } > //------------------------------------------------------------------------- > #endregion > > } >} >