VB.net嵌套循环 - 效率

时间:2011-09-06 02:49:58

标签: vb.net nested-loops

以下哪个循环更快?我已经在网上阅读了各种各样的东西,包括Stack Overflow上的很多内容,我仍然不确定.net代码的答案是什么?是否通过.net中的字节码编译器进行了一些自动优化?我在这里发现了类似的帖子,但是关于Java。 Efficiency of nested Loop

For n = 1 to 1000
   For m = 1 to 2000
       A(n,m) = b(n,m)
 Next m
Next n

或者,切换订单:

For m = 1 to 2000
   For n = 1 to 1000
       A(n,m) = b(n,m)
 Next n
Next m 

由于它在内存中的顺序,它是否更快......如果是这样的话?

2 个答案:

答案 0 :(得分:1)

我有我的TickTimer课程,所以我决定尝试一下。 我不得不增加阵列的大小以注意差异 为自己测试一下。第一个确实更快。

Module Module1

    Sub Main()
        Dim A(10000, 20000) As Int16
        Dim b(10000, 20000) As Int16

        For n = 1 To 10000
            For m = 1 To 20000
                A(n, m) = 1
                b(n, m) = 1
            Next m
        Next n

        Dim firstTick As TickTimer = New TickTimer()
        For n = 1 To 10000
            For m = 1 To 20000
                A(n, m) = b(n, m)
            Next m
        Next n
        Console.WriteLine(firstTick.DeltaSeconds(""))

        Dim secondTick As TickTimer = New TickTimer()
        For m = 1 To 20000
            For n = 1 To 10000
                A(n, m) = b(n, m)
            Next n
        Next m
        Console.WriteLine(secondTick.DeltaSeconds(""))

        Console.ReadKey()

    End Sub

End Module


Public Class TickTimer
    Public currentTicks As Long
    Public lastTicks As Long = System.DateTime.Now.Ticks
    Public retVal As String
    ''' <summary>
    ''' Calculates the seconds it took since the class was instantiated until this method
    ''' is first invoked and for subsequent calls since the previous time the method was called
    ''' </summary>
    ''' <param name="message">Message (e.g. "The last query took ")</param>
    ''' <returns>The passed string followed by the seconds: "          The last query took,     0.3456"</returns>
    ''' <remarks>To see how long it takes a method to execute instantiate this class at its
    ''' very begining and call this method just before it returns; Log the result with     Debug.Writeln or something similar</remarks>
    Public Function DeltaSeconds(ByVal message As String) As String
        currentTicks = System.DateTime.Now.Ticks
        retVal = String.Format("{0}, {1}", message.PadLeft(100), ((currentTicks - lastTicks) /     TimeSpan.TicksPerSecond).ToString().PadRight(15))
        lastTicks = currentTicks
        Return retVal
    End Function
End Class

答案 1 :(得分:0)

Rick Sladkey完美地说明了这一点! 以前的测试首先滚动相同的数组&#34;按行&#34;然后&#34;按列&#34;。下面是正确的测试,它对数组及其转置执行相同的操作。 在这种情况下,我们可以看到实际上For调用的开销有成本,第二种方法稍微快一些。如果我们通过列&#34;滚动数组&#34;这会产生更大数量级的计算时间,这肯定是不被注意的。我不知道有关CLI数组的详细信息以及为什么这是处理器的行为,但这是证据。

Structure mt
    Dim m() As Double
End Structure

Sub Main()

    Dim a, b As Integer
    Dim p As Integer = 10000000
    Dim q As Integer = 5
    Dim m1(p, q) As Double
    Dim m4(q, p) As Double
    'Dim m2()() As Double
    'Dim m3() As mt

    'ReDim m2(p)
    'For a = 1 To p
    '    ReDim m2(a)(q)
    'Next

    'ReDim m3(p)
    'For a = 1 To p
    '    ReDim m3(a).m(q)
    'Next

    Dim sw As New Stopwatch

    sw.Restart()
    For a = 1 To p
        For b = 1 To q
            m1(a, b) = 0
            'm2(a)(b) = 0
            'm3(a).m(b) = 0
        Next
    Next
    sw.Stop()

    Console.WriteLine("Small loop in large loop: " & sw.Elapsed.ToString())

    sw.Restart()
    For a = 1 To q
        For b = 1 To p
            'm1(b, a) = 0
            'm2(b)(a) = 0
            'm3(b).m(a) = 0
            m4(a, b) = 0
        Next
    Next
    sw.Stop()

    Console.WriteLine("Large loop in small loop: " & sw.Elapsed.ToString())
    Stop
End Sub