2012-08-23 81 views
-1

我有這個函數,它可以根據可用庫存計算某些產品的建議價格,現在它不重要,但它以某種方式運行,而我的程序運行速度更快,沒有它我真的很困惑,我不知道爲什麼它很慢VB.net你會如何提高這個功能的性能

Dim MinPrice As Double 
    Dim VAT = 1.1899999999999999 
    Dim margin1 
    Dim potherrule1 As String 
    Dim margin2 
    Dim potherrule2 As String 
    Dim margin3 
    Dim potherrule3 As String 
    Dim defaultmargin 

    If SupplierMargin IsNot Nothing Then 
     margin1 = SupplierMargin(0) 
     potherrule1 = SupplierPother(0) 
     margin2 = SupplierMargin(1) 
     potherrule2 = SupplierPother(1) 
     margin3 = SupplierMargin(2) 
     potherrule3 = SupplierPother(2) 
     defaultmargin = SupplierMargin(3) 
    End If 


    If IsDBNull(CurrentPother) Or (potherrule1 = "x" And potherrule2 = "x" And potherrule3 = "x") Then 
     MinPrice = Math.Round((oprice/(1 - defaultmargin)) * VAT, 2) 
     Return MinPrice 
    End If 

    If Not IsDBNull(CurrentPother) Then 
     If potherrule1 <> "x" Then 
      Dim v1 As Integer 
      Dim v2 As Integer 
      Dim rule As String = potherrule1 
      Dim parts() As String = rule.Split(New String() {" bis "}, StringSplitOptions.None) 
      v1 = Integer.Parse(parts(0)) 
      v2 = Integer.Parse(parts(1)) 
      If CurrentPother >= v1 And CurrentPother <= v2 Then 
       MinPrice = Math.Round((oprice/(1 - margin1)) * VAT, 2) 
      End If 
      Return MinPrice 

     ElseIf potherrule2 <> "x" Then 
      Dim v1 As Integer 
      Dim v2 As Integer 
      Dim rule As String = potherrule2 
      Dim parts() As String = rule.Split(New String() {" bis "}, StringSplitOptions.None) 
      v1 = Integer.Parse(parts(0)) 
      v2 = Integer.Parse(parts(1)) 
      If CurrentPother >= v1 And CurrentPother <= v2 Then 
       MinPrice = Math.Round((oprice/(1 - margin2)) * VAT, 2) 
       Return MinPrice 
      End If 

     ElseIf potherrule2 <> "x" Then 
      Dim v1 As Integer 
      Dim v2 As Integer 
      Dim rule As String = potherrule3 
      Dim parts() As String = rule.Split(New String() {" bis "}, StringSplitOptions.None) 
      v1 = Integer.Parse(parts(0)) 
      v2 = Integer.Parse(parts(1)) 
      If CurrentPother >= v1 And CurrentPother <= v2 Then 
       MinPrice = Math.Round((oprice/(1 - margin3)) * VAT, 2) 
       Return MinPrice 
      End If 
     Else 
      MinimumPriceWhenPother4IsDBnull(SupplierMargin, oprice) 
     End If 
    End If 

你可以請建議一些改進,可以使此功能更快?

+2

將'Option Strict'設置爲on,避免'ArrayList',而不是'SupplierMargin'使用'List(Of Double)','SupplierPother'使用'List(Of String)'。 –

+0

我發送到這個函數arraylist保存在My.Setting我似乎無法找到'System.Collections.Generic.List(Of Double)'在My.Settings – user1570048

+0

@ user1570048:好的,但即使這樣仍然有可能。 http://stackoverflow.com/questions/951876/can-you-have-a-generic-listof-t-in-your-settings-file –

回答

0

除了上面@Tim Schmelter的評論,你確實意識到你的第三個條件仍然在看potherrule2嗎?因此,我的猜測是,當potherrule3是<>「x」並且您希望它進行評估時,您的代碼默認爲MinimumPriceWhenPother4IsDBnull函數。不知道該功能是幹什麼的,但是如果運行很多,這可能會導致速度問題。

0

我同意@APrough問題可能是由於您的第三個ElseIf做出了錯誤的比較,然後該方法意外降至MinimumPriceWhenPother4IsDBnull(SupplierMargin, oprice)。你在做什麼?在這一點上你是否可以從數據庫中讀取數據?

您的前三個If處理程序中的每一個都可以分解爲另一種方法來增加代碼重用。儘管如此,它不會提高性能。

另一個警告點。注意Math.Round。默認情況下,它使用ANSI標準舍入到最近的偶數,而不是您在學校學到的四捨五入。見http://www.thinqlinq.com/Post.aspx/Title/Rounding-in-.Net。儘管如此,這不會成爲10倍性能下降的原因。

此外,您可能想要在VS終極中運行性能分析,或者抓住螞蟻分析器的eval來查看代碼中真正瓶頸的位置。它通常可以被事件處理程序,方法調用或其他意想不到的地方隱藏。