Skip to content

Instantly share code, notes, and snippets.

@tompazourek
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tompazourek/2a9afa555945c3b9fe5d to your computer and use it in GitHub Desktop.
Save tompazourek/2a9afa555945c3b9fe5d to your computer and use it in GitHub Desktop.
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
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