Last active
August 29, 2015 14:02
-
-
Save tompazourek/2a9afa555945c3b9fe5d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub SomeMethod() | |
Using Watch.Measure() | |
' other code | |
End Using ' Output: [Watch] [SomeMethod] xx.xx ms | |
Using Watch.Measure("customLabel") | |
' other code | |
End Using ' Output: [Watch] [customLabel] xx.xx ms | |
End Sub | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Diagnostics | |
Imports System.Globalization | |
Public Module Watch | |
Public Function Measure(Optional label As String = Nothing) As IDisposable | |
Return New InternalWatch(label) | |
End Function | |
Private Class InternalWatch | |
Implements IDisposable | |
Private _label As String | |
Private _startTicks As Long | |
Private _endTicks As Long | |
Public Sub New(label As String) | |
If label Is Nothing Then | |
_label = New StackFrame(2, True).GetMethod().Name | |
Else | |
_label = label | |
End If | |
_startTicks = DateTime.Now.Ticks | |
End Sub | |
Public Overloads Sub Dispose() Implements IDisposable.Dispose | |
If _endTicks <> 0 Then Return | |
_endTicks = DateTime.Now.Ticks | |
Dim duration = TimeSpan.FromTicks(_endTicks - _startTicks) | |
Debug.WriteLine(String.Format("[Watch] [{0}] {1} ms", _label, duration.TotalMilliseconds.ToString("0.00", CultureInfo.InvariantCulture))) | |
End Sub | |
End Class | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment