A generic benchmark routine

You often need to benchmark a piece of code, which you usually do as follows:

Dim start As Date = Now' insert here the code to benchmarkDim elapsed As TimeSpan = Now.Subtract(start)Console.WriteLine("Total time: {0} secs.", elapsed)

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:

Delegate Sub BenchmarkDelegate()Enum BenchmarkModes    DontShow    Console    MessageBoxEnd EnumFunction BenchmarkIt(ByVal routine As BenchmarkDelegate, _    ByVal mode As BenchmarkModes, Optional ByVal msg As String = Nothing) As _    TimeSpan    ' remember starting time    Dim start As Date = Now    ' run the procedure to be benchmarked    routine.Invoke()    ' evaluate elapsed time, assign to result    Dim elapsed As TimeSpan = Now.Subtract(start)    ' exit if nothing else to do    If mode = BenchmarkModes.DontShow Then Return elapsed     ' build a suitable string if none was provided    If msg Is Nothing OrElse msg.Length = 0 Then        ' use the name of the target method        msg = routine.Method.Name    End If    ' append a placeholder for elapsed time, if not there    If msg.IndexOf("{0}") < 0 Then        msg &= ": {0} secs"    End If    ' display on the console window or a message box    If mode = BenchmarkModes.Console Then        Console.WriteLine(msg, elapsed)    ElseIf mode = BenchmarkModes.MessageBox Then        MessageBox.Show(String.Format(msg, elapsed))    End If    ' return result to caller    Return elapsedEnd Function

Here’s an example that benchmarks an array fill routine and displays the result on the console window. Notice that we pass Nothing as the msg argument, and the routine uses the name of the target method as the default:

Sub ArrayFill()    Dim i As Integer    Dim arr(100000) As Integer    For i = 0 To UBound(arr)        arr(i) = i    NextEnd SubSub Main()    BenchmarkIt(AddressOf ArrayFill, BenchmarkModes.Console)End Sub

In this second example, we display the elapsed time with a custom message in a message box:

    BenchmarkIt(AddressOf ArrayFill, BenchmarkModes.MessageBox, _        "Array filling takes {0} seconds.")

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a