VB.NET Benchmark Programs

The VB.NET programming language

You want to determine the performance of a certain subroutine in the VB.NET language. By using For loops with a Stopwatch instance, we can time millions of calls and then compute the total number of nanoseconds per call. We benchmark VB.NET subs.

This VB article provides a benchmark harness. The code determines the average time required for a single Function call.

Implementation

Note

This is the benchmark harness. You can see we use the Stopwatch.StartNew function around each For loop. The For loops here execute the subs A and B a total of ten million times each. If you are timing a slower subroutine, you can adjust that total down.

Stopwatch Example For Loop Examples
Program that benchmarks Subs [VB.NET]

Module Module1

    Sub Main()
	Dim m As Integer = 10000000
	Dim s1 As Stopwatch = Stopwatch.StartNew
	For i As Integer = 0 To m - 1
	    A()
	Next
	s1.Stop()

	Dim s2 As Stopwatch = Stopwatch.StartNew
	For i As Integer = 0 To m - 1
	    B()
	Next
	s2.Stop()

	Dim u As Integer = 1000000
	Console.WriteLine(((s1.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns"))
	Console.WriteLine(((s2.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns"))
    End Sub

    Sub A()
	Dim a As Integer = Integer.Parse("0")
    End Sub

    Sub B()
	Dim b As Integer = Integer.Parse("0") + Integer.Parse("0")
    End Sub
End Module

Output

112.22 ns
224.98 ns

Computing nanoseconds. Finally, we compute the average number of nanoseconds per function call. We multiply the milliseconds by one million to convert to total nanoseconds; then we divide by the number of subroutine invocations to get the average nanoseconds.

Integer.Parse Function Examples

Note: Please remove the method bodies of the A() and B() subroutines. The Integer.Parse calls are there to demonstrate how the benchmark harness is used. Each Integer.Parse call happens to require 112 nanoseconds.

Summary

Though in most cases benchmarks of the C# language and the VB.NET language will yield identical results, some constructs are only available in one language. For example, the VB.NET With statement is not available in the C# language. In these situations, a VB.NET benchmarking harness is handy.

VB.NET Tutorials
.NET