0

I need to choice a way to write a piece of code that will repeated very much so I want it to be as fast as possible.

This is my code (at the moment):

For Cel As Short = 0 To Cels - 1
    Rw = Int(Cel / 3) + 1
    Col = Cel - ((Rw - 1) * 3) + 1
    RifTxt = TabW(RifTW("Pi")).Cells(Cel)
    If Col = 1 Then
        RowArr(Col) = RifTxt
    Else
        If DateTime.TryParse(RifTxt, culture, Styles, DateResult) Then
            RowArr(Col) = DateResult.ToString("yyyy-MM-dd")
        Else
            RowArr(Col) = "0000-00-00"
        End If
        If Col = 3 Then
            RowArr(3) = Rw
            Pi_W.Rows.Add (RowArr)
        End If
    End If
Next Cel

I'm wondering changing it (using ElseIf Or Select Case) as shown:

Using ElseIf

For Cel As Short = 0 To Cels - 1
    Rw = Int(Cel / 3) + 1
    Col = Cel - ((Rw - 1) * 3) + 1
    RifTxt = TabW(RifTW("Pi")).Cells(Cel)
    If Col = 1 Then
        RowArr(Col) = RifTxt
    ElseIf Col = 2
        If DateTime.TryParse(RifTxt, culture, Styles, DateResult) Then
            RowArr(Col) = DateResult.ToString("yyyy-MM-dd")
        Else
            RowArr(Col) = "0000-00-00"
        End If
    ElseIf Col = 3 Then
        If DateTime.TryParse(RifTxt, culture, Styles, DateResult) Then
            RowArr(Col) = DateResult.ToString("yyyy-MM-dd")
        Else
            RowArr(Col) = "0000-00-00"
        End If
        RowArr(3) = Rw
        Pi_W.Rows.Add (RowArr)
    End If
Next Cel

Or using Select Case:

For Cel As Short = 0 To Cels - 1
    Rw = Int(Cel / 3) + 1
    Col = Cel - ((Rw - 1) * 3) + 1
    RifTxt = TabW(RifTW("Pi")).Cells(Cel)
    Select Case Col
    Case is = 1 Then
        RowArr(Col) = RifTxt
    Case is = 2
        If DateTime.TryParse(RifTxt, culture, Styles, DateResult) Then
            RowArr(Col) = DateResult.ToString("yyyy-MM-dd")
        Else
            RowArr(Col) = "0000-00-00"
        End If
    Case is  = 3 Then
        If DateTime.TryParse(RifTxt, culture, Styles, DateResult) Then
            RowArr(Col) = DateResult.ToString("yyyy-MM-dd")
        Else
            RowArr(Col) = "0000-00-00"
        End If
        RowArr(3) = Rw
        Pi_W.Rows.Add (RowArr)
    End Select
Next Cel

I tryed to compare the three methods on .netFiddle but I'm not sure of results.

Can you please suggest me the right choice?

genespos
  • 3,211
  • 6
  • 38
  • 70
  • I would use a function that returned the value based on the `Col` variable you pass to it. That would make this more readable. – OneFineDay Nov 19 '15 at 19:49
  • you've got the code, NET has a hi-res Stopwatch, just do each in a loop and time them (do it in Release mode). You might want to change the loop iterator to Int32/Integer. – Ňɏssa Pøngjǣrdenlarp Nov 19 '15 at 19:49
  • @Plutonix Are you saying me that using `For Cel As Integer` would make code faster than using `For Cel As Short`? – genespos Nov 19 '15 at 19:54

1 Answers1

0

In this case, your Select and If examples are most likely going to compile to the same machine code, with the exact same performance.

In any case: Do not worry about micro-optimizing like this unless you have shown that it is a problem section of code. Write something that is readable and maintainable, first and foremost. The difference in performance one way or another is going to be negligable.

As always, if you want to know what performs better, try it and see.

Glorin Oakenfoot
  • 2,455
  • 1
  • 17
  • 19