[RESOLVED] Quick question on which code is more efficent.-VBForums
Results 1 to 5 of 5

Thread: [RESOLVED] Quick question on which code is more efficent.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    66

    Resolved [RESOLVED] Quick question on which code is more efficent.

    I recently re wrote a small piece of code, and was wondering if I made the code better or worse when I changed it.

    Orignal Code:
    Code:
    Select Case wcoCoverage.LevelID 'Level ID = Policy or Vehicle Id's
                  Case wcoWP_CNAuto.ID
                    Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, False)
                  Case Else 'Vehicle id
                    If Not p_dictdatVehicles.ContainsKey(wcoCoverage.LevelID) Then Continue For
                    If swcoCoverageCode = "VFTP" Then 'Vehicle Limits Total Premium 
                      Dim datVehicle As DILCCA.Vehicle = p_dictdatVehicles(wcoCoverage.LevelID)
                      Dim decPreValue As Decimal = GetCoveragePremium(wcoCompany, wcoCoverage.ID)
                      If decPreValue <> Decimal.Zero Then datVehicle.TotalPremiumPrincipal = decPreValue.ToASCurrency
                      Continue For
                    End If
                    Select Case True
                      Case lstCodesSurcharges.Contains(swcoCoverageCode)
                        Update_CNAutoSurcharges(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedSurchargesIds, swcoCoverageCode)
                      Case lstCodesDiscounts.Contains(swcoCoverageCode)
                        Update_CNAutoDiscounts(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedDiscountsIds, swcoCoverageCode)
                      Case dictVehicleLimitCodeandIds.ContainsValue(swcoCoverageCode)
                        Update_CNAutoVehicleLimits(datApplicationDetail, wcoCoverage, wcoCompany, dictVehicleLimitCodeandIds, swcoCoverageCode)
                      Case lstCodesPolicyChangeForms.Contains(swcoCoverageCode)
                        Update_CNAutoPolicyChangeForm(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedPolicyFormsIds, sIssusingstate, swcoCoverageCode)
                      Case Else 'Adds to (Auto->Vehicle->Optional Add Coverages/OPCF)
                        Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, True)
                    End Select
                End Select
    New code:
    Code:
    Select Case True
                  Case wcoWP_CNAuto.ID = wcoCoverage.LevelID 'Policy Level 
                    Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, False)
                  Case p_dictdatVehicles.ContainsKey(wcoCoverage.LevelID) 'Vehicle Level (Will be in Dictionary)
                    Select Case True
                      Case swcoCoverageCode = "VFTP"
                        Dim datVehicle As DILCCA.Vehicle = p_dictdatVehicles(wcoCoverage.LevelID)
                        Dim decPreValue As Decimal = GetCoveragePremium(wcoCompany, wcoCoverage.ID)
                        If decPreValue <> Decimal.Zero Then datVehicle.TotalPremiumPrincipal = decPreValue.ToASCurrency
                      Case lstCodesSurcharges.Contains(swcoCoverageCode)
                        Update_CNAutoSurcharges(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedSurchargesIds, swcoCoverageCode)
                      Case lstCodesDiscounts.Contains(swcoCoverageCode)
                        Update_CNAutoDiscounts(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedDiscountsIds, swcoCoverageCode)
                      Case dictVehicleLimitCodeandIds.ContainsValue(swcoCoverageCode)
                        Update_CNAutoVehicleLimits(datApplicationDetail, wcoCoverage, wcoCompany, dictVehicleLimitCodeandIds, lstUsedVehicleLimitsIds, swcoCoverageCode)
                      Case lstCodesPolicyChangeForms.Contains(swcoCoverageCode)
                        Update_CNAutoPolicyChangeForm(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedPolicyFormsIds, sIssusingstate, swcoCoverageCode)
                      Case Else
                        Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, True)
                    End Select
                End Select

    This code basically sits in a for loop, and it first determines if the data belongs to a Policy or a Vehicle, If it's a policy there is only one place it can go, if it's a Vehicle then I determine what the code is and put it in the correct spot.

    What I was wondering, would I have been better to do a:
    A) IF Statment, Then use a Case in the else part
    B) Used two if statements
    C) Should just stuck with the orignal code?
    D) It doesn't matter?

    Are using Select Case True good coding? I've read they are but I've also read that they are not, I understand it's everone personally choice..
    The goal is just to make it the most efficent, so I was just curious if I handled it correctly.

    Thanks

  2. #2
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,792

    Re: Quick question on which code is more efficent.

    The answer is D. Between those two, the efficiency difference won't amount to anything in a million iterations. Select case may be less efficient in certain situations, and more efficient in others, but it really doesn't make a difference.
    My usual boring signature: Nothing

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,006

    Re: Quick question on which code is more efficent.

    Wow, I never knew you could use Select Case like that. Learned something new here today.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    2,467

    Re: Quick question on which code is more efficent.

    There shouldn't be any measurable difference in the runtime performance, but it's your debug-time performance that matters here. Readability, in other words. I prefer the second code from that viewpoint. I see nothing wrong with Select Case True because I find it easier to read than If with a large number of ElseIfs. However, since the outer Select Case only contains two options you might consider this alternative:
    Code:
    If wcoWP_CNAuto.ID = wcoCoverage.LevelID Then 'Policy Level 
    
         Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, False)
    
    ElseIf p_dictdatVehicles.ContainsKey(wcoCoverage.LevelID) Then	 'Vehicle Level (Will be in Dictionary)
    			
         Select Case True
    	Case swcoCoverageCode = "VFTP"
    		Dim datVehicle As DILCCA.Vehicle = p_dictdatVehicles(wcoCoverage.LevelID)
    		Dim decPreValue As Decimal = GetCoveragePremium(wcoCompany, wcoCoverage.ID)
    		If decPreValue <> Decimal.Zero Then datVehicle.TotalPremiumPrincipal = decPreValue.ToASCurrency
    	Case lstCodesSurcharges.Contains(swcoCoverageCode)
    		Update_CNAutoSurcharges(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedSurchargesIds, swcoCoverageCode)
    	Case lstCodesDiscounts.Contains(swcoCoverageCode)
    		Update_CNAutoDiscounts(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedDiscountsIds, swcoCoverageCode)
    	Case dictVehicleLimitCodeandIds.ContainsValue(swcoCoverageCode)
    		Update_CNAutoVehicleLimits(datApplicationDetail, wcoCoverage, wcoCompany, dictVehicleLimitCodeandIds, lstUsedVehicleLimitsIds, swcoCoverageCode)
    	Case lstCodesPolicyChangeForms.Contains(swcoCoverageCode)
    		Update_CNAutoPolicyChangeForm(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedPolicyFormsIds, sIssusingstate, swcoCoverageCode)
    	Case Else
    		Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, True)
    	End Select
    
    End If

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    66

    Re: Quick question on which code is more efficent.

    I just wanted to thank everyone for there input on the above code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.