Why one by one: UBound or Length

Could there be any specific reason why you can select UBound over Length ?

Here the code and 1-dimensionality are transferred as the second parameter.

  For iIndex = 0 To UBound(myList) If Left(Request.ServerVariables("REMOTE_ADDR"), Len(myList(iIndex))) = saIPList(iIndex) Then bAuth = True Exit For End If Next 

Any performance improvement over Length

+8
source share
5 answers

They do different things! UBound gives you the last index in the array, and Length gives you the length. This is not the same, because usually UBound will be Length - 1 .

+12
source share

Ubound exists primarily for backward compatibility with old code. I didn’t see anything, saying that it was outdated, but at the same time I admit that it is not entirely consistent with how they use the language in recent years. The same is true for the Len () and Left () functions in this code; it is the path of the past, not the future. The sooner you adapt, the happier you will be.

For what it's worth, the argument is pretty much controversial. More "modern" ways to write this code look completely different. Here is one example:

 bAuth = myList.Zip(saIPList, Function(a,b) New With {.Length = a.Length, .saIP = b} ) _ .Any(Function(i) Request.ServerVariables("REMOTE_ADDR").ToString().SubString(0,i.Length) = i.saIP) 
+4
source share

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.

+3
source share

It is carried over from the earlier days of VB. UBound can give you the highest index of any single dimension in a multidimensional array. Length gives only the total number of elements.

If you declare:

 ' A 5x10 array: Dim array(4, 9) As Integer 

Values:

 array.Length = 50 ' Total number of elements. array.Rank = 2 ' Total number of dimensions. array.LBound(0) = 0 ' Minimum index of first dimension. array.LBound(1) = 0 ' Minimum index of second dimension. array.UBound(0) = 4 ' Maximum index of first dimension. array.UBound(1) = 9 ' Maximum index of second dimension. 
+2
source share

The interesting UBounds in this example are a little faster.

  Dim startTick As Long For j As Integer = 1 To 5 Dim rnd As New Random(Now.Subtract(Now.Date).TotalSeconds) Dim RandomMax As Integer = rnd.Next(10000, 100000) Dim cumbersome() As Integer = New Integer() {rnd.Next} 'Ubound is better than Length. Using Length takes as much time as redimensioning the entire array. startTick = Environment.TickCount For i As Integer = 1 To RandomMax - 1 ReDim Preserve cumbersome(UBound(cumbersome) + 1) cumbersome(UBound(cumbersome)) = Rnd.Next Next Debug.Print("{0}) Array Method UBound: {1} Ticks to construct {2} integers", j, Environment.TickCount - startTick, RandomMax) 'Length is slow don't use it startTick = Environment.TickCount For i As Integer = 1 To RandomMax - 1 ReDim Preserve cumbersome(cumbersome.Length) cumbersome(cumbersome.Length - 1) = Rnd.Next Next Debug.Print("{0}) Array Method Length: {1} Ticks to construct {2} integers", j, Environment.TickCount - startTick, RandomMax) Next 
0
source share

All Articles