I came across the following code recently and would like to optimize it:
Public Shared Function ComputeLabel(ByVal Action As Integer, ByVal Flag1 As Boolean, ByVal Flag2 As Boolean) As String
Dim Prefix As String = ""
If Flag2 Then
Prefix = "Conditional "
ElseIf Flag1 Then
Prefix = "Held "
End If
Select Case Action
Case 0
Return ""
Case 1
Return Prefix & "Cancelled"
Case 2
Return Prefix & "Discontinued"
Case 3
Return Prefix & "Suspended"
Case 4
Return Prefix & "Unsuspended"
Case 6
Return Prefix & "Collected"
Case 7
Return Prefix & "Released from Hold"
Case 8
Return Prefix & "Modified"
Case 9
Return Prefix & "Discontinued for the Future"
Case 10
Return Prefix & "Verified"
Case 11
Return Prefix & "Modified And Verified"
Case 12
Return "Hold " & Prefix & "Cancelled"
Case Else
Return ""
End Select
End Function
Note that Action 0 is the most common case.
Okay, I’ve already cleaned up this function a bit–it was using a variable and returning it at the end, and using Return seems better. But additionally, I think it would be better code to build an array at the beginning of the report execution, and then just access array elements each time this function is called, instead of using a Select statement. But case 12 is making things more complicated (as you can see, it adds the prefix in the middle instead of at the beginning.)
What do you think would be the best way:
-
One time building a 39-element array for the three cases:
Private Shared OrderActions() As String = {"", "Cancelled", ...}Then in the function accessing it like so:
If Action < 0 OrElse Action >= 13 Then Return "" Return OrderActions(Action - Flag2 * 13 - (Flag1 AndAlso Not Flag2) * 26) -
Using a 13-element array with a Replace (something like
Return Replace(LabelList(Action), "{Prefix}", Prefix)?) -
Using a 12-element array with a special case for Action 12.
-
Something else I haven’t thought of.
?
Update 1: my formatting was off so the options might have been unclear. It should be more readable now.
Update 2: I see what you mean that from a performance perspective, fully expanding all the cases and using simple variable assignment is probably fastest. So… let’s say top speed is not the priority, but overall elegance is (a combination of clean code and speed). Any chance people could give their take on that, too? I’ll vote everyone up who gives reasonable help for all aspects of the question.
Update 3: One additional consideration I was ignoring is that some non-experienced programmers are going to be maintaining this long-term, so it does need to be easy to understand. I guess my examples of trying to shorten the code really aren’t good from this perspective.
Update 4: TESTING IS KING OVER ALL!!! Once I was inspired to do some speed tests, I got some interesting results. See my answer below.
actually, if you put them into arrays, it brings the execution time from 6589ms on my machine to 1174ms using the following method:
Below is from a console app, but you get the general idea. The arrays are loaded once, then accessed as many times as you want, in this case I was using the for loop that Steve Wortham posted as a test.