比较Excel和delphi的计算速度

时间:2013-09-30 16:02:15

标签: algorithm excel delphi comparison

我在excel中创建了一个宏,它需要花费大量时间来计算在一定高度范围内组织书籍堆叠的所有可能性。所有的书都有自己的尺寸。 随着书籍数量的增加,问题变得越来越复杂,并且有更多的解决方案,因此计算时间增加。直到它实际上冻结了excel行中空间的耗尽。

现在我的问题是,如果我使用Delphi Borland执行此操作,我是否会遇到同样的问题,或者delphi程序可以更快地执行此操作? 或者有没有办法更快更有效地做到这一点? 我对此并不熟悉,所以如果问题需要解释,我会非常感激!

Public Text, Alpha, Result, Temp_Result, Wgt, Hgt, Combo_No As Integer, Allwd_Combo As Integer
Public Max_Wgt As Double, Max_Hgt As Double, Crt_Wgt, Crt_Hgt
Sub ListPermutationsOrCombinations()
    Dim Str_Len As Integer, Len_Text As Integer, TotalComb As Integer
    Len_Text = Worksheets("Listed Books").Range("A65536").End(xlUp).Row - 1
    Worksheets("Buildups").Range("A2:C65536").Clear
    Next_Row = 1
    Max_Wgt = Worksheets("constraints").Range("B1")
    Max_Hgt = Worksheets("constraints").Range("B2")

    ReDim Alpha(1 To Len_Text)
    For j = 1 To Len_Text
            Alpha(j) = Worksheets("Listed Books").Cells(j + 1, 1)
    Next j

    For i = 1 To Len_Text
        Str_Len = i


        TotalComb = Application.WorksheetFunction.Combin(Len_Text, Str_Len)
        Allwd_Combo = TotalComb
        ReDim Result(1 To TotalComb)
        ReDim Wgt(1 To TotalComb)
        ReDim Hgt(1 To TotalComb)

        ReDim Temp_Result(1 To Str_Len)

        Combo_No = 1

        AddCombination Len_Text, Str_Len

       Worksheets("Buildups").Cells(Next_Row + 1, 1).Resize(TotalComb) = Application.Transpose(Result)
       Worksheets("Buildups").Cells(Next_Row + 1, 2).Resize(TotalComb) = Application.Transpose(Wgt)
       Worksheets("Buildups").Cells(Next_Row + 1, 3).Resize(TotalComb) = Application.Transpose(Hgt)
       Next_Row = Next_Row + Allwd_Combo
    Next i
End Sub


Private Sub AddCombination(Optional PopSize As Integer = 0, _
                          Optional SetSize As Integer = 0, _
                          Optional NextMember As Integer = 0, _
                          Optional NextItem As Integer = 0)

Static iPopSize As Integer
Static iSetSize As Integer
Static SetMembers() As Integer
Dim i As Integer

If PopSize <> 0 Then
    iPopSize = PopSize
    iSetSize = SetSize
    ReDim SetMembers(1 To iSetSize) As Integer
    ReDim Crt_Wgt(1 To iSetSize) As Double
    ReDim Crt_Hgt(1 To iSetSize) As Double
    NextMember = 1
    NextItem = 1
End If

For i = NextItem To iPopSize
    SetMembers(NextMember) = i
    Crt_Wgt(NextMember) = Worksheets("Listed Books").Cells(i + 1, 2)
    Crt_Hgt(NextMember) = Worksheets("Listed Books").Cells(i + 1, 3)
    If NextMember <> iSetSize Then
        AddCombination , , NextMember + 1, i + 1
    Else
        If (Application.WorksheetFunction.Sum(Crt_Wgt) > Max_Wgt) Or _
            (Application.WorksheetFunction.Sum(Crt_Hgt) > Max_Hgt) Then
            Allwd_Combo = Allwd_Combo - 1
        Else
            SavePermutation SetMembers(), iSetSize, Combo_No
            Combo_No = Combo_No + 1
        End If
    End If
Next i



End Sub 'AddCombination

Sub SavePermutation(Set_Member, Str_Len As Integer, Combo_No As Integer)
For i = 1 To Str_Len
    Temp_Result(i) = Alpha(Set_Member(i))
Next i

Result(Combo_No) = Join(Temp_Result)
Wgt(Combo_No) = Application.WorksheetFunction.Sum(Crt_Wgt)
Hgt(Combo_No) = Application.WorksheetFunction.Sum(Crt_Hgt)

End Sub

0 个答案:

没有答案