|
|
|
You often need to benchmark a piece of code, which you usually do as follows: |
Thanks to delegates, you can write a reusable routine that can benchmark any Sub that takes zero arguments. This generic benchmark routine returns the TimeSpan value and optionally displays a default or a custom message, either on the console window or in a message box: |
Click here to copy the following block | Delegate Sub BenchmarkDelegate()
Enum BenchmarkModes DontShow Console MessageBox End Enum
Function BenchmarkIt(ByVal routine As BenchmarkDelegate, _ ByVal mode As BenchmarkModes, Optional ByVal msg As String = Nothing) As _ TimeSpan Dim start As Date = Now routine.Invoke() Dim elapsed As TimeSpan = Now.Subtract(start) If mode = BenchmarkModes.DontShow Then Return elapsed If msg Is Nothing OrElse msg.Length = 0 Then msg = routine.Method.Name End If If msg.IndexOf("{0}") < 0 Then msg &= ": {0} secs" End If If mode = BenchmarkModes.Console Then Console.WriteLine(msg, elapsed) ElseIf mode = BenchmarkModes.MessageBox Then MessageBox.Show(String.Format(msg, elapsed)) End If Return elapsed End Function |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|