为什么一个在另一个:UBound或Length

时间:2011-09-01 20:21:04

标签: vb.net

有没有具体的理由可以选择UBound而不是Length

以下是代码和1维作为第二个参数传递。

    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

针对Length

的任何表现增益

5 个答案:

答案 0 :(得分:12)

他们做不同的事情! UBound为您提供数组中的最后一个索引,而Length为您提供长度。这些不一样,因为通常UBound将为Length - 1

答案 1 :(得分:4)

Ubound的存在主要是为了向后兼容旧代码。我还没有看到任何说它已经被弃用的东西,但与此同时我认识到它与他们近年来采用该语言的方式并不完全一致。对于该代码中的Len()和Left()函数也是如此;他们是过去的方式,而不是未来。你越早适应,你就会越快乐。

对于它的价值,这个论点在很大程度上没有实际意义。编写代码的更“现代”方式看起来完全不同。这是一个例子:

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)

答案 2 :(得分:3)

为了获得性能提升,我还对哪种功能具有最佳性能感兴趣。

似乎长度-1比UBound快得多。我预计UBound会以某种方式更快。<​​/ p>

在100.000.000次之后,似乎长度-1的时间是952ms,而对于UBound:5844ms。

(长度-1)比UBound快~6倍

用于测试的代码

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

尝试不同的方法来消除编译器的可能优化,所有这些都具有与上述相同的结果。

答案 3 :(得分:2)

它是从早期的VB时代开始的。 UBound可以为您提供多维数组中任何单个维度的最高索引。长度仅为您提供元素总数。

如果您声明:

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

值为:

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.

答案 4 :(得分:0)

在这个例子中,有趣的UBounds速度要快得多

  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