C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: If you are timing a slower subroutine, you can adjust that total down. This also depends on your processor's speed.
VB.NET program that benchmarks Subs
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
Then: We divide by the number of subroutine invocations to get the average nanoseconds.
Integer.ParseNote: Please remove the method bodies of A() and B(). The Integer.Parse calls are there to demonstrate how the benchmark harness is used.
And: Each Integer.Parse call happens to require 112 nanoseconds, but this is not important to know.