将数组元素存储为变量与保留数组元素的..next循环的效率

时间:2019-05-24 10:22:16

标签: arrays excel vba performance element

问题:kmatch(c)存储在e中并使用e是否更有效?请解释原因。

背景: 在为运行代码节省时间时,上面的问题浮现在脑海。

有关数组大小的信息: kmatch()将数据从1到excel工作表的最后一行存储1列。通常,它有几千行,kmatchoutputc在子例程中较早地定义为变体

当前代码:

For c = 1 To UBound(kmatch, 1)
    If kmatch(c) <> 0 Then
        outputc(c, 1) = Abs(Other_array1(kmatch(c), 1))
        outputc(c, 2) = Abs(Other_array2(kmatch(c), 1))
        outputc(c, 3) = Abs(Other_array3(kmatch(c), 1))
        outputc(c, 5) = Other_array4(c, 1)

        If (Abs(Other_array5(kmatch(c), 1)) - Abs(Other_array6(kmatch(c), 1))) > 0 Then
            outputc(c, 4) = Abs(Other_array5(kmatch(c), 1)) - Abs(Other_array6(kmatch(c), 1))
        Else
            outputc(c, 4) = 0
        End If

        For b = 1 To 5
            outputc(c, 6) = outputc(c, b) + outputc(c, 6)
        Next b

        Other_array7(c, 1) = Other_array8(kmatch(c), 1)
    Else
        For b = 1 To 6
            outputc(c, b) = 0
        Next b
    End If
Next c

计划的代码:

For c = 1 To UBound(kmatch, 1)
    e = kmatch(c)
    If e <> 0 Then
        outputc(c, 1) = Abs(Other_array1(e, 1))
        outputc(c, 2) = Abs(Other_array2(e, 1))
        outputc(c, 3) = Abs(Other_array3(e, 1))
        outputc(c, 5) = Other_array4(c, 1)

        If (Abs(Other_array5(e, 1)) - Abs(Other_array6(e, 1))) > 0 Then
            outputc(c, 4) = Abs(Other_array5(e, 1)) - Abs(Other_array6(e, 1))
        Else
            outputc(c, 4) = 0
        End If

        For b = 1 To 5
            outputc(c, 6) = outputc(c, b) + outputc(c, 6)
        Next b

        Other_array7(c, 1) = Other_array8(e, 1)
    Else
        For b = 1 To 6
            outputc(c, b) = 0
        Next b
    End If
Next c

0 个答案:

没有答案