To improve performance, I was also interested in which feature has the best performance.
The length -1 seems to be much faster than UBound. I expected UBound to be somehow faster.
After 100,000,000 times, it seems that the time for length -1 is 952ms and for UBound: 5844ms.
(length -1) ~ 6 times faster than UBound
Code used for testing
Private Sub UboundlengthToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UboundlengthToolStripMenuItem.Click ListBox1.Items.Clear() ListBox1.Items.Add("BEGIN") 'set required vars Dim ints() As Integer = {1, 2, 3, 4, 5} 'end vars setting Dim t As New Stopwatch Dim gt As New Stopwatch Dim time1 As Integer Dim temp As Integer Dim d As Double = GC.GetTotalMemory(False) GC.Collect() GC.WaitForPendingFinalizers() GC.Collect() GC.GetTotalMemory(False) ListBox1.Items.Add("Free Memory: " & d) gt.Start() t.Reset() 'starting test--------------------------------------- 'single test--------------------------------------- t.Start() For i As Integer = 0 To TextBox1.Text temp = ints(ints.Length - 1) Next t.Stop() time1 = t.ElapsedMilliseconds ListBox1.Items.Add("arr.length - 1") ListBox1.Items.Add("Func1 total time: " & time1) ListBox1.Items.Add("Func1 single time: " & time1 / TextBox1.Text) t.Reset() 'single test--------------------------------------- 'single test--------------------------------------- t.Start() For i As Integer = 0 To TextBox1.Text temp = ints(UBound(ints)) Next t.Stop() time1 = t.ElapsedMilliseconds ListBox1.Items.Add("UBound:") ListBox1.Items.Add("Func1 total time: " & time1) ListBox1.Items.Add("Func1 single time: " & time1 / TextBox1.Text) t.Reset() 'single test--------------------------------------- 'Finishing test-------------------------------------- gt.Stop() ListBox1.Items.Add("Total time " & gt.ElapsedMilliseconds) d = GC.GetTotalMemory(True) - d ListBox1.Items.Add("Total Memory Heap consuming (bytes)" & d) ListBox1.Items.Add("END") End Sub
I tried different things to eliminate the possible optimality of the compiler, all with the same result as above.
Niels kool
source share