Executetime Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. IO; Namespace Testproject1.performance { Public Static Class Executetime {# region setout Private Static Textwriter _ out; Public Static Textwriter out { Get { If (_ Out = Null ) {Setout (console. Out );} Return _ Out ;}} Public Static Void Setout (textwriter newout ){ If (Newout =Null ){ Throw New Argumentnullexception (" Newout ");} _ Out = newout;} # endregion # Region enable Static Bool ? _ Enable; /// <Summary> /// Enable or not /// </Summary> Public Static Bool Enable {Get { If (_ Enable! = Null ) Return _ Enable. value; _ enable = False ;# If Debug _ enable = True ; # Endif Return _ Enable. value ;} Set {_ Enable = Value ; }}# Endregion # region tabindexStatic Int Tabindex; /// <Summary> /// Tab counter /// </Summary> Internal Static Int Tabindex { Get { Return Tabindex ;} Set { If ( Value <0) tabindex = 0; Else Tabindex = Value ; }}# Endregion Static String Topretab ( String Content ){ For ( Int I = 0; I <tabindex; I ++) {content =" \ T "+ Content ;} Return Content ;} Public Static Void Writeline ( String Content) {out. writeline (tabindex. tostring () + topretab (content ));}}}
Executetimeitem Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. diagnostics;Namespace Testproject1.performance { Public Class Executetimeitem: idisposable {stopwatch Sw = New Stopwatch (); Object Content; Internal Executetimeitem ( Object Content) {executetime. tabindex ++; executetime. writeline ( String . Format (" Start [{0 }] ", Content )); This . Content = content; Sw. Start () ;}# region idisposable Member Bool Disposed = False ; Public Void Dispose () {dispose ( True );} Public Void Dispose ( Bool Disposed ){ If (! This . Disposed) {Sw. Stop (); executetime. writeline (String . Format (" When [{0}] is ended: {1} milliseconds. ", Content, SW. elapsed. totalmilliseconds); executetime. tabindex --; disposed = True ; }}# Endregion }}
Test [Testmethod]Public VoidTestmethod1 (){For(IntI = 0; I <10; I ++ ){Using(NewExecutetimeitem (I) {thread. Sleep (10 );For(IntJ = 0; j <10; j ++ ){Using(NewExecutetimeitem (j) {thread. Sleep (20 );}}}}}
result 1 start [0] 2 start [0] 2 end [0] When: 20.4351 milliseconds. 2 start [1] 2 end [1] Time: 19.4521 milliseconds. 2 start [2] 2 end [2] Time: 19.9381 milliseconds. 2 start [3] 2 end [3] Time: 19.9483 milliseconds. 2 start [4] 2 end [4] Time: 19.9731 milliseconds. 2 start [5] 2 end [5] Time: 19.9782 milliseconds. 2 start [6] 2 end [6] Time: 20.0924 milliseconds. 2 start [7] 2 end [7] Time: 19.8172 milliseconds. 2 start [8] 2 end [8] Time: 19.9806 milliseconds. 2 start [9] 2 end [9] Time: 19.9507 milliseconds. 1. End Time [0]: 210.6243 milliseconds. 1 start [1] 2 start [0] 2 end [0] Use Time: 19.9719 milliseconds. 2 start [1] 2 end [1] Time: 20.0503 milliseconds. 2 start [2] 2 end [2] Time: 20.3831 milliseconds. 2 start [3] 2 end [3] Time: 19.4919 milliseconds. 2 start [4] 2 end [4] Time: 20.068 milliseconds. 2 start [5] 2 end [5] Time: 19.8372 milliseconds. 2 start [6] 2 end [6] Time: 19.9735 milliseconds. 2 start [7] 2 end [7] Time: 19.9735 milliseconds. 2 start [8] 2 end [8] Time: 19.9609 milliseconds. 2 start [9] 2 end [9] Time: 19.9688 milliseconds. 1 end [1] Time: 209.9757 milliseconds. 1 start [2] 2 start [0] 2 end [0] Time: 19.9991 milliseconds. 2 start [1] 2 end [1] Time: 19.9814 milliseconds. 2 start [2] 2 end [2] Time: 19.977 milliseconds. 2 start [3] 2 end [3] Time: 19.9444 milliseconds. 2 start [4] 2 end [4] Time: 19.9767 milliseconds. 2 start [5] 2 end [5] Time: 19.9676 milliseconds. 2 start [6] 2 end [6] Time: 20.3405 milliseconds. 2 start [7] 2 end [7] Time: 19.6065 milliseconds. 2 start [8] 2 end [8] Time: 19.9841 milliseconds. 2 start [9] 2 end [9] Time: 19.927 milliseconds. 1 end [2] Time: 210.0064 milliseconds. 1 start [3] 2 start [0] 2 end [0] Time: 19.9767 milliseconds. 2 start [1] 2 end [1] Time: 19.9881 milliseconds. 2 start [2] 2 end [2] Time: 19.9558 milliseconds. 2 start [3] 2 end [3] Time: 19.9684 milliseconds. 2 start [4] 2 end [4] Time: 20.2941 milliseconds. 2 start [5] 2 end [5] Time: 19.6356 milliseconds. 2 start [6] 2 end [6] Time: 19.9668 milliseconds. 2 start [7] 2 end [7] Time: 19.9877 milliseconds. 2 start [8] 2 end [8] Time: 19.9735 milliseconds. 2 start [9] 2 end [9] Time: 19.9924 milliseconds. 1. End Time [3]: 210.0265 milliseconds. 1 start [4] 2 start [0] 2 end [0] Time: 20.0034 milliseconds. 2 start [1] 2 end [1] Time: 19.9928 milliseconds. 2 start [2] 2 end [2] Time: 19.9463 milliseconds. 2 start [3] 2 end [3] Time: 19.9656 milliseconds. 2 start [4] 2 end [4] Time: 19.9664 milliseconds. 2 start [5] 2 end [5] Time: 19.9782 milliseconds. 2 start [6] 2 end [6] Time: 19.9684 milliseconds. 2 start [7] 2 end [7] Time: 19.964 milliseconds. 2 start [8] 2 end [8] Time: 20.0117 milliseconds. 2 start [9] 2 end [9] Time: 19.9227 milliseconds. 1 end [4] Time: 209.9635 milliseconds. 1 start [5] 2 start [0] 2 end [0] Time: 19.9979 milliseconds. 2 start [1] 2 end [1] Time: 20.0231 milliseconds. 2 start [2] 2 end [2] Time: 19.9353 milliseconds. 2 start [3] 2 end [3] Time: 19.9495 milliseconds. 2 start [4] 2 end [4] Time: 19.9688 milliseconds. 2 start [5] 2 end [5] Time: 20.1204 milliseconds. 2 start [6] 2 end [6] Time: 19.8148 milliseconds. 2 start [7] 2 end [7] Time: 19.9924 milliseconds. 2 start [8] 2 end [8] Time: 19.9621 milliseconds. 2 start [9] 2 end [9] Time: 19.9526 milliseconds. 1. End Time [5]: 210.006 milliseconds. 1 start [6] 2 start [0] 2 end [0] Time: 19.9798 milliseconds. 2 start [1] 2 end [1] Time: 19.9613 milliseconds. 2 start [2] 2 end [2] Time: 19.9774 milliseconds. 2 start [3] 2 end [3] Time: 20.0255 milliseconds. 2 start [4] 2 end [4] Time: 19.8664 milliseconds. 2 start [5] 2 end [5] Time: 19.9794 milliseconds. 2 start [6] 2 end [6] Time: 19.9495 milliseconds. 2 start [7] 2 end [7] Time: 20.0361 milliseconds. 2 start [8] 2 end [8] Time: 19.914 milliseconds. 2 start [9] 2 end [9] Time: 20.3673 milliseconds. 1. End Time [6]: 210.4049 milliseconds. 1 start [7] 2 start [0] 2 end [0] Use Time: 19.9794 milliseconds. 2 start [1] 2 end [1] Time: 19.9774 milliseconds. 2 start [2] 2 end [2] Time: 19.9778 milliseconds. 2 start [3] 2 end [3] Time: 19.9719 milliseconds. 2 start [4] 2 end [4] Time: 19.9676 milliseconds. 2 start [5] 2 end [5] Time: 20.0341 milliseconds. 2 start [6] 2 end [6] Time: 19.5978 milliseconds. 2 start [7] 2 end [7] Time: 19.979 milliseconds. 2 start [8] 2 end [8] Time: 19.9487 milliseconds. 2 start [9] 2 end [9] Time: 23.1245 milliseconds. 1. End Time [7]: 212.7541 milliseconds. 1 start [8] 2 start [0] 2 end [0] Use Time: 19.9983 milliseconds. 2 start [1] 2 end [1] Time: 19.9755 milliseconds. 2 start [2] 2 end [2] Time: 19.9747 milliseconds. 2 start [3] 2 end [3] Time: 19.9936 milliseconds. 2 start [4] 2 end [4] Time: 19.9451 milliseconds. 2 start [5] 2 end [5] Time: 19.9893 milliseconds. 2 start [6] 2 end [6] Time: 19.9526 milliseconds. 2 start [7] 2 end [7] Time: 19.8892 milliseconds. 2 start [8] 2 end [8] Time: 19.9723 milliseconds. 2 start [9] 2 end [9] Time: 19.9743 milliseconds. 1. End Time [8]: 209.8386 milliseconds. 1 start [9] 2 start [0] 2 end [0] Use Time: 19.2914 milliseconds. 2 start [1] 2 end [1] Time: 19.9704 milliseconds. 2 start [2] 2 end [2] Time: 19.9613 milliseconds. 2 start [3] 2 end [3] Time: 19.9841 milliseconds. 2 start [4] 2 end [4] Time: 20.0731 milliseconds. 2 start [5] 2 end [5] Time: 19.8557 milliseconds. 2 start [6] 2 end [6] Time: 19.9668 milliseconds. 2 start [7] 2 end [7] Time: 19.9546 milliseconds. 2 start [8] 2 end [8] Time: 20.0338 milliseconds. 2 start [9] 2 end [9] Time: 19.9113 milliseconds. 1. End Time [9]: 210.0209 milliseconds.