VB.net你将如何提高这个功能的性能

时间:2012-08-23 22:09:46

标签: vb.net performance

我有这个功能,根据可用的库存计算某些产品的建议价格,现在它不重要但它不知道但是我的程序运行得像x10更快没有它我真的很困惑,我不知道为什么它很慢

 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 个答案:

答案 0 :(得分:0)

除了@Tim Schmelter上面的评论,你确实意识到你的第三个条件仍然是在看potherrule2?因此,我的猜测是,当potherrule3为&lt;&gt;时,您的代码默认为您的MinimumPriceWhenPother4IsDBnull函数。 “x”并且您希望它进行评估。不确定该功能是做什么的,但是如果它运行很多,那可能会导致你的速度问题。

答案 1 :(得分:0)

我同意@APrough的意见,问题可能是由于你的第三个ElseIf进行了错误的比较,然后该方法意外地降至MinimumPriceWhenPother4IsDBnull(SupplierMargin, oprice)。那个方法你在做什么?你有可能在那时从数据库中读取吗?

前三个If处理程序中的每一个都可以被分解为另一种方法来增加代码重用。但它不会增加穿孔。

另一个警告点。注意Math.Round。默认情况下,它使用ANSI标准舍入到最近的偶数而不是在学校学到的舍入。见http://www.thinqlinq.com/Post.aspx/Title/Rounding-in-.Net。但这不会导致10倍的性能下降。

此外,您可能希望在VS ultimate中运行性能分析,或者获取Ants剖析器的评估,以查看代码中真正的瓶颈所在。它通常可以被事件处理程序,方法调用或其他意外的地方隐藏。

相关问题