Wednesday, December 05, 2007

Getting the name of the current sub or function

System.Reflection.MethodBase.GetCurrentMethod.Name

Very cool!

Timing SQL queries using TimeSpan

I recently had a web page that ran a very extensive and complicated multi-part SQL query. The performance sucked. I needed to come up with a way of determining which query was taking the bulk of the time, and then work to improve it. Luckily I built each query command within it's own Function that returned a string (The final query was assembled using a System.Text.tringBuilder object). My research finally led me to a handy VB.Net class called "TimeSpan" (System.TimeSpan). The code is below.


Private Sub MySub()

Dim startTime As DateTime
Dim endTime As DateTime
Dim tsExecution As TimeSpan
startTime = Now

...


endTime = Now
tsExecution = endTime.Subtract(startTime)
Response.Write(System.Reflection.MethodBase.GetCurrentMethod.Name & _
" took: " & tsExecution.Milliseconds & _
" ms to complete!
")

End Sub